i have users and each user have these data for exmp :
userOne = [
fruits : ['banana','apple','pineapple','somethingElse'],
colors : ['red','blue','green'],
habit : ['read','sing','sleep']
]
i want to return something like this :
fruits: {hasBanana:11, hasPineapple:22, ......}
colors: {hasRed:11, hasBlue:0, ......}
habit : {hasRead:2, hasSleep:10}
i was going to do it this way
db.getCollection('users').aggregate([
{$group : {_id: {tags:"$banana"}}},
{$match : {"_id.banana":"banana"}},
{$count : 'hasBanana'}
])
will return this:
{
"hasBanana" : 59
}
and repeat it manytime but i believe there is short way to do it