My list contains various unique items -
search = alluk.distinct('Object of search')
I am interested in counting each item individually. Presently, I am counting them manually in the following way -
alluk.find({'Object of search':'Offensive weapons'}).count()
Query
Is there a way to iterate through the search array and count each item one by one?
I have attempted the following -
alluk.find({'Object of search':{'$in': search}}).count()
However, this approach does not achieve exactly what I desire.
SOLUTION -
for item in alluk.aggregate([
{ '$match': { 'Object of search': { '$in': objectofsearch }}},
{ '$group': {
'_id': '$Object of search',
'count': { '$sum': 1 }
}}
]):
print(item)