鍍金池/ 問答/數(shù)據(jù)庫/ Querying an array in MongoDB?

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.

回答
編輯回答
青檸

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" ] }
2017年3月25日 00:17
編輯回答
淺淺
db.multiArr.find({ 
  Keys: "apple"
});

https://docs.mongodb.com/manu...

2018年8月9日 14:22