有趣的问题,这将做用于检查在一个阵列中的元素的指定匹配表达式匹配的特技
db.multiArr.find({'Keys':{$elemMatch:{$elemMatch:{$in:['carrot']}}}})
$elemMatch
。如果你想找到的第一位置数据的阵列,使用0作为参考指标嵌套阵列获取数据 所以嵌套$elemMatch
将进入更深的嵌套阵列
测试数据
db.multiArr.insert({"ID" : "fruit1","Keys" : [["apple", "carrot", "banana"]]})
db.multiArr.insert({"ID" : "fruit2","Keys" : [["apple", "orange", "banana"]]})
db.multiArr.find({'Keys':{$elemMatch:{$elemMatch:{$in:['carrot']}}}})
{ "_id" : ObjectId("506555212aeb79b5f7374cbf"), "ID" : "fruit1", "Keys" : [ [ "apple", "carrot", "banana" ] ] }
db.multiArr.find({'Keys':{$elemMatch:{$elemMatch:{$in:['banana']}}}})
{ "_id" : ObjectId("506555212aeb79b5f7374cbf"), "ID" : "fruit1", "Keys" : [ [ "apple", "carrot", "banana" ] ] }
{ "_id" : ObjectId("5065587e2aeb79b5f7374cc0"), "ID" : "fruit2", "Keys" : [ [ "apple", "orange", "banana" ] ] }
我认为你最好的选择是改变你的模式。包含数组数组的文档不被Mongo很好的支持。 – JohnnyHK
@JohnnyHK,它很可能在mongo中查询嵌套数组。看看我的回答 – RameshVel