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'}]
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With