Nik*_* B. 8 arrays mongoose mongodb mongodb-query
我的db集合中有一个folowing对象结构:
{
"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"
}
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
所以"value"属性可以是空字符串,布尔值,数组甚至根本没有定义.
如何查询列出具有非空属性数组的对象,并且在数组内至少有一个对象内没有定义"attributes.value"属性?
ps我试过以下查询:
db.collection.find({"attributes": {$exists: true, $ne: []}, "attributes.value": {$exists: false}})
Run Code Online (Sandbox Code Playgroud)
但查询结果为空.
Not*_*d4U 13
$ elemMatch运算符匹配包含数组字段的文档,其中至少有一个元素匹配所有指定的查询条件.
这个查询对我有用:
db.getCollection('testeur').find({ "attributes": {
$exists: true,
$ne: [],
$elemMatch: { "value": {$exists: false } }
}
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5309 次 |
| 最近记录: |