我不确定问题标题是否正确。无论如何,我的收藏中的文档如下所示:
{
properties: [
{name:a, value:w},
{name:b, value:x},
{name:c, value:y},
{name:d, value:z}
]
}
如何查找符合此条件的文档:“(其中properties.name = a,properties.value = x)AND(其中properties.name = c,properties.value = y)”?
答案 0 :(得分:1)
尝试以下查询。请注意,子文档{name:“b”,value:“x”}中元素的顺序很重要:
db.foo.find({properties:{$all:[{name:"b", value:"x"}, {name:"c", value:"y"}]}})
或者下面的查询更好(子文档的顺序无关紧要):
db.foo.find({properties:{$all:[{$elemMatch:{name:"b", value:"x"}}, {$elemMatch:{name:"c", value:"y"}}]}})