mongodb计数每个字段/键的不同值的数量

Lia*_*atz 90 mongodb mongodb-query aggregation-framework

是否存在用于计算字段在DB中包含多少个不同值的查询.

我有一个国家的领域,有8种国家价值观(西班牙,英格兰,法国等...)

如果有人在新国家/地区添加了更多文档,我希望查询返回9.

是否有更容易的方式然后分组和计数?

Ste*_*nie 171

MongoDB有一个distinct命令,它返回一个字段的不同值数组; 您可以检查数组的长度以进行计数.

还有一个shell db.collection.distinct()助手:

> db.countries.distinct('country');
[ "Spain", "England", "France", "Australia" ]

> db.countries.distinct('country').length
4
Run Code Online (Sandbox Code Playgroud)

  • 如果您的不同值的数量太高,这种方法确实不起作用......如果您正在查看世界上某些人的不同名称或其他内容.你有答案可以扩展吗? (37认同)
  • 1+长度.我正在努力寻找类似的东西.谢谢. (3认同)
  • 我不知道为什么他们也不在那里使用 count() (2认同)
  • @MarianKlühspies - 因为它只是一个 JavaScript 数组,它使用 length 属性来计算元素的数量。 (2认同)

exp*_*ert 97

以下是使用聚合API的示例.为了使案例复杂化,我们使用来自文档的数组属性的不区分大小写的单词进行分组.

db.articles.aggregate([
    {
        $match: {
            keywords: { $not: {$size: 0} }
        }
    },
    { $unwind: "$keywords" },
    {
        $group: {
            _id: {$toLower: '$keywords'},
            count: { $sum: 1 }
        }
    },
    {
        $match: {
            count: { $gte: 2 }
        }
    },
    { $sort : { count : -1} },
    { $limit : 100 }
]);
Run Code Online (Sandbox Code Playgroud)

给出结果如

{ "_id" : "inflammation", "count" : 765 }
{ "_id" : "obesity", "count" : 641 }
{ "_id" : "epidemiology", "count" : 617 }
{ "_id" : "cancer", "count" : 604 }
{ "_id" : "breast cancer", "count" : 596 }
{ "_id" : "apoptosis", "count" : 570 }
{ "_id" : "children", "count" : 487 }
{ "_id" : "depression", "count" : 474 }
{ "_id" : "hiv", "count" : 468 }
{ "_id" : "prognosis", "count" : 428 }
Run Code Online (Sandbox Code Playgroud)

  • 登录只是为了+这个答案。谢谢!顺便说一句,如果你是在一个独特的领域做这件事,只需删除展开线。 (3认同)

w33*_*33b 21

我想要一个更简洁的答案,我使用聚合和组中的文档想出了以下内容

db.countries.aggregate([{"$group": {"_id": "$country", "count":{"$sum": 1}}}])
Run Code Online (Sandbox Code Playgroud)


chr*_*dam 13

使用MongoDb 3.4.4和更新版本,您可以利用$arrayToObject运算符和$replaceRoot管道来获取计数.

例如,假设您拥有一组具有不同角色的用户,并且您希望计算角色的不同计数.您需要运行以下聚合管道:

db.users.aggregate([
    { "$group": {
        "_id": { "$toLower": "$role" },
        "count": { "$sum": 1 }
    } },
    { "$group": {
        "_id": null,
        "counts": {
            "$push": { "k": "$_id", "v": "$count" }
        }
    } },
    { "$replaceRoot": {
        "newRoot": { "$arrayToObject": "$counts" }
    } }    
])
Run Code Online (Sandbox Code Playgroud)

示例输出

{
    "user" : 67,
    "superuser" : 5,
    "admin" : 4,
    "moderator" : 12
}
Run Code Online (Sandbox Code Playgroud)


Rea*_*aas 10

我使用这个查询:

var collection = "countries"; var field = "country"; 
db[collection].distinct(field).forEach(function(value){print(field + ", " + value + ": " + db[collection].count({[field]: value}))})
Run Code Online (Sandbox Code Playgroud)

输出:

countries, England: 3536
countries, France: 238
countries, Australia: 1044
countries, Spain: 16
Run Code Online (Sandbox Code Playgroud)

该查询首先区分所有值,然后计算每个值出现的次数。


eva*_*rix 9

您可以利用Mongo Shell Extensions.$HOME/.mongorc.js如果你在Node.js/io.js中进行编码,它可以附加到你的或者以编程方式附加的单个.js导入.

样品

对于字段计数的每个不同值,可选地按查询过滤文档中的出现次数

> db.users.distinctAndCount('name', {name: /^a/i})

{
  "Abagail": 1,
  "Abbey": 3,
  "Abbie": 1,
  ...
}
Run Code Online (Sandbox Code Playgroud)

field参数可以是字段数组

> db.users.distinctAndCount(['name','job'], {name: /^a/i})

{
  "Austin,Educator" : 1,
  "Aurelia,Educator" : 1,
  "Augustine,Carpenter" : 1,
  ...
}
Run Code Online (Sandbox Code Playgroud)


Vim*_*mal 5

为了找到一个与众不同的field_1集合,但我们还需要一些WHERE条件,例如:

db.your_collection_name.distinct('field_1', {WHERE condition here and it should return a document})

因此,找出names年龄大于25的集合与之不同的数字是:

db.your_collection_name.distinct('names', {'age': {"$gt": 25}})

希望能帮助到你!