本文介绍了MongoDB对象属性$存在于嵌套数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库集合中有以下对象结构:

I have a folowing object structure in my db collection:

{
    "name" : "test",
    "code" : "test",
    "attributes" : [
        {
            "name" : "test1",
            "code" : "code1"
        },
        {
            "name" : "test2",
            "code" : "code2",
            "value" : true
        },
        {
            "name" : "test3",
            "code" : "code3",
            "value" : ""
        },
        {
            "name" : "test4",
            "code" : "code4"
            "value" : [
                {
                    "code" : "code4.1",
                    "name" : "test4.1"
                },
                {
                    "name" : "test4.2"
                }
            ]
        }
    ]
}

因此,值"属性可以是空字符串,布尔值,数组,甚至根本没有定义.

So "value" property can be empty string, boolean, array or even not defined at all.

如何查询以列出具有非空属性数组且在数组内至少一个对象内未定义"attributes.value"属性的对象?

How can I make query to list objects that have non-empty attributes array and don't have "attributes.value" property defined inside at least one object inside array?

p.s.我尝试了以下查询:

p.s. I tried following query:

db.collection.find({"attributes": {$exists: true, $ne: []}, "attributes.value": {$exists: false}})

但查询结果为空.

推荐答案

此查询对我有用:

db.getCollection('testeur').find({ "attributes": {
        $exists: true,
        $ne: [],
        $elemMatch: { "value": {$exists: false } }
    }
})

这篇关于MongoDB对象属性$存在于嵌套数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 05:36