I have these 3 requests:
db.mycollection.count({requestA:{$exists:true}})
db.mycollection.count({requestB:{$exists:true}})
db.mycollection.count({requestC:{$exists:true}})
I'd like 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 the error:
"errmsg" : "exception: invalid operator '$exists'",
I guess that we cannot use $exists with $project.
Any tips about a good approach ? Thank you