mongodb:查询嵌套字符串数组

时间:2019-02-09 09:28:15

标签: mongodb mongodb-query nested-queries

我有类似的对象列表

[
   { item: "journal", instock: [ { warehouse: "A", qty: 5 }, { warehouse: "C", qty: 15, type : ["hello", "world", "wassup", "yo"] } ] },
   { item: "notebook", instock: [ { warehouse: "C", qty: 5 } ] },
   { item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 1515, type : ["hello", "wassup", "yo"] } ] },
   { item: "planner", instock: [ { warehouse: "A", qty: 40 }, { warehouse: "B", qty: 515, type : ["hello"] } ] },
   { item: "postcard", instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 3515, type : ["wassup", "yo"] } ] }
]

如何查询“类型”中包含“ hello”的所有仓库的列表

db.inventory.find( { "instock": { $elemMatch: {type :["hello"]}}} , {_id:0})

返回只有问候的对象。

1 个答案:

答案 0 :(得分:2)

Dot notation works in this case, try:

db.collection.find({ "instock.type": "hello"})

MongoDB playground