Querying an array in MongoDB?

Test data
   

db.multiArr.insert({"ID" : "fruit1","Keys" : [["carrot", "banana"]]})
db.multiArr.insert({"ID" : "fruit2","Keys" : ["apple", "orange"]})
db.multiArr.insert({"ID" : "fruit2","Keys" : ["apple"]})

How do I query for Keys in ["apple", "orange", "carrot"]?
Need

 ({"ID" : "fruit2","Keys" : ["apple", "orange"]})
 ({"ID" : "fruit2","Keys" : ["apple"]})

Thanks.

阅读 2k
2 个回答

If this is what you meant

db.multiArr.find({Keys: {$in: ["apple", "orange", "carrot"]}})
{ "_id" : ObjectId("5a6fcb43943f5c239f98fbe6"), "ID" : "fruit2", "Keys" : [ "apple", "orange" ] }
{ "_id" : ObjectId("5a6fcb44943f5c239f98fbe7"), "ID" : "fruit2", "Keys" : [ "apple" ] }
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进