我有一个具有以下模式的集合:
db.posts.insert({sents:
[
{uni:['a','b','c'],bi:[['a','b'],['b','c']]},
{uni:['x','y','z'],bi:[['x','y'],['y','z']]}
]})
下面的查询返回上面插入的记录:
> db.posts.find({'sents.bi':{'$elemMatch':{'$in':['a']}}})
{
"_id" : ObjectId("537595f254bae6dfabddf0c9"),
"sents" : [
{
"uni": [ "a", "b", "c" ],
"bi": [[ "a", "b" ], [ "b", "c" ] ]
},
{
"uni": [ "x", "y", "z" ],
"bi": [ [ "x", "y" ], [ "y", "z" ] ]
}
]
}
然而,当我在'sent .bi'上创建索引后:
>db.posts.ensureIndex({'sents.bi':1})
上面的查询停止工作:
> db.posts.find({'sents.bi':{'$elemMatch':{'$in':['a']}}}).count()
0
我知道我做错了什么,我只是不知道我做错了什么:)
谢谢你,
因为上面列出的查询没有给我任何有或没有索引的东西。我在想,你的意思可能是:
db.posts.find({'sents.bi':{'$elemMatch': {$elemMatch: {$in: ['a']}}}})
注意你得到了一个数组中的数组。您必须执行两次$elemMatch。