mongodb 计算每个字段/键的不同值的数量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14924495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mongodb count num of distinct values per field/key
提问by Liatz
Is there a query for calculating how many distinct values a field contains in DB.
是否有用于计算一个字段在 DB 中包含多少个不同值的查询。
f.e I have a field for country and there are 8 types of country values (spain, england, france, etc...)
fe 我有一个国家字段,有 8 种国家/地区值(西班牙、英格兰、法国等...)
If someone adds more documents with a new country I would like the query to return 9.
如果有人添加了更多带有新国家/地区的文档,我希望查询返回 9。
Is there easier way then group and count?
有没有比分组和计数更简单的方法?
回答by Stennie
MongoDB has a distinct
commandwhich returns an array of distinct values for a field; you can check the length of the array for a count.
MongoDB 有一个distinct
命令,它返回一个字段的不同值的数组;您可以检查数组的长度以进行计数。
There is a shell db.collection.distinct()
helper as well:
还有一个 shelldb.collection.distinct()
助手:
> db.countries.distinct('country');
[ "Spain", "England", "France", "Australia" ]
> db.countries.distinct('country').length
4
回答by expert
Here is example of using aggregation API. To complicate the case we're grouping by case-insensitive words from array property of the document.
这是使用聚合 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 }
]);
that give result such as
给出的结果如
{ "_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 }
回答by chridam
With MongoDb 3.4.4 and newer, you can leverage the use of $arrayToObject
operator and a $replaceRoot
pipeline to get the counts.
使用 MongoDb 3.4.4 及更新版本,您可以利用$arrayToObject
运算符和$replaceRoot
管道的使用来获取计数。
For example, suppose you have a collection of users with different roles and you would like to calculate the distinct counts of the roles. You would need to run the following aggregate pipeline:
例如,假设您有一组具有不同角色的用户,并且您想要计算角色的不同计数。您需要运行以下聚合管道:
db.users.aggregate([
{ "$group": {
"_id": { "$toLower": "$role" },
"count": { "$sum": 1 }
} },
{ "$group": {
"_id": null,
"counts": {
"$push": { "k": "$_id", "v": "$count" }
}
} },
{ "$replaceRoot": {
"newRoot": { "$arrayToObject": "$counts" }
} }
])
Example Output
示例输出
{
"user" : 67,
"superuser" : 5,
"admin" : 4,
"moderator" : 12
}
回答by evandrix
You can leverage on Mongo Shell Extensions. It's a single .js import that you can append to your $HOME/.mongorc.js
, or programmatically, if you're coding in Node.js/io.js too.
您可以利用Mongo Shell 扩展。$HOME/.mongorc.js
如果您也在 Node.js/io.js 中编码,则它是单个 .js 导入,您可以将其附加到您的.
Sample
样本
For each distinct value of field counts the occurrences in documents optionally filtered by query
对于字段的每个不同值,计算文档中的出现次数,可选择按查询过滤
>
db.users.distinctAndCount('name', {name: /^a/i})
>
db.users.distinctAndCount('name', {name: /^a/i})
{
"Abagail": 1,
"Abbey": 3,
"Abbie": 1,
...
}
The field parameter could be an array of fields
field 参数可以是一个字段数组
>
db.users.distinctAndCount(['name','job'], {name: /^a/i})
>
db.users.distinctAndCount(['name','job'], {name: /^a/i})
{
"Austin,Educator" : 1,
"Aurelia,Educator" : 1,
"Augustine,Carpenter" : 1,
...
}
回答by Vimal
To find distinct in field_1
in collection but we want some WHERE
condition too than we can do like following :
要field_1
在集合中找到不同的,但我们也需要一些WHERE
条件,而不是像下面这样:
db.your_collection_name.distinct('field_1', {WHERE condition here and it should return a document})
db.your_collection_name.distinct('field_1', {WHERE condition here and it should return a document})
So, find number distinct names
from a collection where age > 25 will be like :
因此,找到与names
年龄> 25 的集合不同的数字,如下所示:
db.your_collection_name.distinct('names', {'age': {"$gt": 25}})
db.your_collection_name.distinct('names', {'age': {"$gt": 25}})
Hope it helps!
希望能帮助到你!