简体   繁体   English

MongoDb 在包含多个 json object 的嵌套数组上找到操作

[英]MongoDb find opeartion on nested array having multiiple json object in it

I'm having dataset in which json consists of array having multiple json in it.我有一个数据集,其中 json 由其中包含多个 json 的数组组成。

The objects in the array have 4 keys, out of which 1 is missing in some of them.数组中的对象有 4 个键,其中一些缺少 1 个键。

I want to get the documents where all the objects in an array are missing the key.我想获取数组中所有对象都缺少密钥的文档。

eg, From these following documents:例如,从以下这些文件中:

{"test":1,desc:[{"price":1,"abc":"def"},{"price":2,"ac":"def"}]}
{"test":1,desc:[{"price":1,"abc":"def"},{"ac":"def"}]}
{"test":1,desc:[{"abc":"def"},{"ac":"def"}]}

I want to match only the last document.我只想匹配最后一个文档。

Thanks in advance..提前致谢..

You can just use $exists :您可以只使用$exists

db.collection.find(
    {
        "desc.price": {$exists: false}
    }
)

And if you want for other fields as well:如果您还想要其他领域:

db.collection.find(
    {
        $or: [
            {
                "desc.price": {$exists: false}
            },
            {
                "desc.abc": {$exists: false}
            },
            {
                "desc.ac": {$exists: false}
            }
        ]
    }
)

One thing to note is that an empty array, ie desc = [] will always be matched by this query.需要注意的一点是空数组,即desc = []将始终与此查询匹配。 if you want to ensure there's atleast one object use this query:如果你想确保至少有一个 object 使用这个查询:

db.collection.find(
    {
       $and: [
           {
               "desc.price": {$exists: false}
           },
           {
               "desc.0": {$exists: true}
           }
       ]
    }
)

Achieved that, just made a script where i used an array to store the price thing and then used.every method for price to be undefined, it returned me the desired result which i want.实现了这一点,只是制作了一个脚本,我使用一个数组来存储价格的东西,然后使用。每个方法都未定义价格,它返回了我想要的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM