Select document having particular key value pair but not having other key value pair

元气小坏坏 提交于 2019-12-12 17:09:56

问题


I have a collection named "collection".
It has 2 documents shown below -
Document A

 {
                "genericParams" : [
                    {
                        "key" : "sms_email_count",
                        "value" : 3
                    },
                    {
                        "key" : "first_sms_email_time",
                        "value" : NumberLong("1450691202568")
                    },
                    {
                        "key" : "second_sms_email_time",
                        "value" : NumberLong("1450691202568")
                    },
                    {
                        "key" : "third_sms_email_time",
                        "value" : NumberLong("1450691202568")
                    },
                    {
                        "key" : "manual_refund_processed",
                        "value" : "false"
                    }
                ]
            }

Document B

 {
                "genericParams" : [
                    {
                        "key" : "sms_email_count",
                        "value" : 3
                    },
                    {
                        "key" : "first_sms_email_time",
                        "value" : NumberLong("1450691202568")
                    },
                    {
                        "key" : "second_sms_email_time",
                        "value" : NumberLong("1450691202568")
                    },
                    {
                        "key" : "third_sms_email_time",
                        "value" : NumberLong("1450691202568")
                    }
                ]
            }

I want to make a query so that the output is only Document B
THE LOGIC for this query is that i want the document to have the key value pair "key" : "third_sms_email_time" but not "key" : "manual_refund_processed".
Document out here refers to Document A/B. :)

 {
                "genericParams" : [
                    {
                        "key" : "sms_email_count",
                        "value" : 3
                    },
                    {
                        "key" : "first_sms_email_time",
                        "value" : NumberLong("1450691202568")
                    },
                    {
                        "key" : "second_sms_email_time",
                        "value" : NumberLong("1450691202568")
                    },
                    {
                        "key" : "third_sms_email_time",
                        "value" : NumberLong("1450691202568")
                    }
                ]
            }

What i have tried -

db.collection.aggregate([
    {$match: { "genericParams.key": { $exists: true, $nin: [ "manual_refund_processed" ] }, "currentState.genericParams.key": "third_sms_email_time"   }},
    { $project : {
        "genericParams" : 1 
    }}  
])

回答1:


Your query attempt uses the same "key" twice. You cannot do that in an object structure as you are essentially "overwiting" the value of the same key. So the actual query considered is only the "second" condition for that key.

So if you want to have multiple conditions for the same key, then use the $and operator:

db.collection.aggregate([
    { "$match": {
        "$and": [
            { "genericParams.key": { "$exists": true, "$ne": "manual_funds_processed" } },
            { "genericParams.key": "third_sms_email_time" }
        ] 
    },
    // other stages
})

Or since all MongoDB conditions are really "and" arguments by default, you can also specify $eq in this case:

db.collection.aggregate([
    { "$match": {
        "genericParams.key": { 
            "$ne": "manual_refund_processed", 
            "$eq": "third_sms_email_time"
        }
    }},
    // other stages
])

Noting here that there is nothing special about .aggregate() here itself, as it just the base "query" part that is doing the work of document selection.

Also note, that with a "positive" condition present ( the $eq ) it is not really necessary to use the $exists since you are already testing that at least that element needs to match.



来源:https://stackoverflow.com/questions/35238554/select-document-having-particular-key-value-pair-but-not-having-other-key-value

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