选择具有特定键值对但不具有其他键值对的文档 [英] Select document having particular key value pair but not having other key value pair

查看:129
本文介绍了选择具有特定键值对但不具有其他键值对的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为收藏"的收藏.
它有2个文档,如下所示-
文档A

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"
                    }
                ]
            }

文档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")
                    }
                ]
            }

我想进行查询,以便仅输出 文档B
此查询的逻辑是我希望文档具有键值对"key" : "third_sms_email_time",而不是"key" : "manual_refund_processed".
此处的文档指的是文档A/B. :)

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")
                    }
                ]
            }

我尝试过的-

db.collection.aggregate([
    {$match: { "genericParams.key": { $exists: true, $nin: [ "manual_refund_processed" ] }, "currentState.genericParams.key": "third_sms_email_time"   }},
    { $project : {
        "genericParams" : 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.

因此,如果您希望同一键具有多个条件,请使用 $and 运算符:

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
})

或者由于默认情况下所有MongoDB条件实际上都是和"参数,因此您还可以指定

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
])

这里注意.aggregate()本身并没有什么特别之处,因为它只是进行文档选择工作的基础查询"部分.

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.

还要注意,在存在正"条件($eq)的情况下,实际上不必使用$exists,因为您已经在测试至少该元素需要匹配.

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.

这篇关于选择具有特定键值对但不具有其他键值对的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆