在MongoDB中查询数组数组

说,我有这样的文件..

"ID" : "fruit1",
"Keys" : [["apple", "carrot", "banana]]

如何查询Keys =“carrot”。以下语法都无效。

db.myColl.results.find({ "Keys" : "carrot" });
db.myColl.results.find({ "Keys" : [["carrot"]] });

以下工作虽然,但没有帮助。

db.myColl.results.find({ "Keys" : [["apple", "carrot", "banana]]});

指向此查询的任何指针都将有所帮助。谢谢。

有趣的问题,这将做的伎俩

 db.multiArr.find({'Keys':{$elemMatch:{$elemMatch:{$in:['carrot']}}}})

$ elemMatch用于检查数组中的元素是否与指定的匹配表达式匹配。
所以嵌套$ 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" ] ] }
http://stackoverflow.com/questions/12629692/querying-an-array-of-arrays-in-mongodb

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:在MongoDB中查询数组数组