I have these 3 queries:
db.mycollection.count({requestA:{$exists:true}}) db.mycollection.count({requestB:{$exists:true}}) db.mycollection.count({requestC:{$exists:true}})
I want to make only one request ... So I tried the following:
db.mycollection.aggregate( [ { $group: { '_id' : { user_id: '$user_id'}, requestA_count: { $sum: { $cond: [ {requestA:{'$exists':true}}, 1, 0 ] } }, requestB_count: { $sum: { $cond: [ {requestB:{'$exists':true}}, 1, 0 ] } }, requestC_count: { $sum: { $cond: [ {requestC:{'$exists':true}}, 1, 0 ] } }, } }, { $project: { _id: 0, user_id: '$_id.user_id', requestA_count: 1, requestB_count: 1, requestC_count: 1 } } ] );
But I got an error:
"errmsg" : "exception: invalid operator '$exists'",
I assume that we cannot use $ exists with $ project.
Any tips on a good approach? Thanks you
mongodb mongodb-query aggregation-framework
John smith
source share