Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB get count of distinct values

Lets say I have a collection of records:

{'name':'record1', 'colors':['red','green','blue']}
{'name':'record2', 'colors':['red','orange']}
{'name':'record3', 'colors':['red','yellow','blue']}
{'name':'record4', 'colors':['red','green','blue']}

I can get a list of distinct colors using:

collection.distinct('colors')
#returns
['red','green','blue','orange','yellow']

is it possible to get a count of the records that those values occur in?

For example:

[{'count':4,'color':'red'},{'count':2,'color':'green'}]
like image 676
corycorycory Avatar asked Feb 06 '23 06:02

corycorycory


1 Answers

An aggregation pipeline using the $group stage like this:

db.collectionName.aggregate( 
  { $unwind: "$colors" }, 
  { $group: { "_id": "$colors", "count": { $sum: 1 } } }, 
  { $project: { "color": "$_id", "count": 1 } }
);

for your documents will result into

{ "count" : 1, "color" : "yellow" }
{ "count" : 1, "color" : "orange" }
{ "count" : 3, "color" : "blue" }
{ "count" : 2, "color" : "green" }
{ "count" : 4, "color" : "red" }

Don't forget to change collection name.

like image 115
tarashypka Avatar answered Apr 05 '23 16:04

tarashypka