Mongodb counts the number of different values ​​for a field / key

Is there a query to calculate the number of different values ​​that the field contains in the database.

fe I have a field for a country, and there are 8 types of country values ​​(Spain, England, France, etc.)

If someone adds more documents to a new country, I would like the request to return 9.

Is there an easier way to group and count?

+63
mongodb mongodb-query aggregation-framework
Feb 17 '13 at 18:27
source share
6 answers

MongoDB has a distinct command that returns an array of different values ​​for a field; you can check the length of the array for counting.

There is a helper db.collection.distinct() shell file:

 > db.countries.distinct('country'); [ "Spain", "England", "France", "Australia" ] > db.countries.distinct('country').length 4 
+136
Feb 18 '13 at 2:43
source share

Here is an example of using the aggregation API. To complicate the case, we group the case-insensitive words from the document array property.

 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 } ]); 

which give a result, for example

 { "_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 } 
+71
Oct 29 '15 at 15:37
source share

You can use Mongo Shell Extensions . This is one .js import that you can add to your $HOME/.mongorc.js or programmatically if you code in Node.js / io.js too.

Example

For each individual value of the calculation of the entry fields in the documents, optionally filtered by request

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

 { "Abagail": 1, "Abbey": 3, "Abbie": 1, ... } 

Field parameter can be an array of fields

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

 { "Austin,Educator" : 1, "Aurelia,Educator" : 1, "Augustine,Carpenter" : 1, ... } 
+7
May 13 '15 at 13:03
source share

With MongoDB 3.4.4 and later, you can use the $arrayToObject operator and the $replaceRoot pipeline to get samples.

For example, suppose you have a collection of users with different roles, and you want to calculate a different number of roles. You will 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" } } } ]) 

Output example

 { "user" : 67, "superuser" : 5, "admin" : 4, "moderator" : 12 } 
+7
Feb 08 '18 at 22:37
source share

To find the distinguishing feature in field_1 in the collection, but we also want to get the WHERE we can do as follows:

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

So, find the number of great names from the collection where age> 25 would look like this:

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

Hope it helps!

+4
Feb 23 '18 at 5:45
source share
 db.collectionName.distinct("fieldName").length 

will work for sure

In my case, it worked. currently counted and allocated together in MongoDB .

0
Nov 29 '17 at 10:59 on
source share



All Articles