MongoDB, Multiple count (with $exists)

穿精又带淫゛_ 提交于 2019-12-05 20:33:20

问题


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


回答1:


You had the right basic idea but $exists is a query condition so is only valid witin a $match. What you want is the $ifNull operator to essentially do the same thing:

db.mycollection.aggregate( [
    { "$group": {
        "_id" : { "user_id": "$user_id" },
        "requestA_count": { "$sum": {
            "$cond": [ { "$ifNull": ["$requestA", false] }, 1, 0 ]
        } },
        "requestB_count": { "$sum": {
            "$cond": [ { "$ifNull": ["$requestB", false] }, 1, 0 ]
        } },
        "requestC_count": { "$sum": {
            "$cond": [ { "$ifNull": ["$requestC", false] }, 1, 0 ]
        } },
    } },
    { "$project": {
        "_id": 0,
        "user_id": "$_id.user_id",
        "requestA_count": 1,
        "requestB_count": 1,
        "requestC_count": 1
    } }
] );

So the $ifNull either returns the present value of the field if it exists or the "right side" argument is returned if it does not. The returned value other than false is intrepreted as being true ( unless of course the value is actually false ).

Essentially this gives you the same functionality of logically testing for the existence of a property in the document.



来源:https://stackoverflow.com/questions/24182379/mongodb-multiple-count-with-exists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!