I have an array of distinct items -
search = alluk.distinct('Object of search')
I am looking to do a count for each item. Currently I am doing them manually like so -
alluk.find({'Object of search':'Offensive weapons'}).count()
Question
Is it possible to loop through the search array counting for each item in turn?
I have tried -
alluk.find({'Object of search':{'$in': search}}).count()
However this isn't exactly what I am after.
SOLUTION -
for item in alluk.aggregate([
{ '$match': { 'Object of search': { '$in': objectofsearch }}},
{ '$group': {
'_id': '$Object of search',
'count': { '$sum': 1 }
}}
]):
print(item)