mongodb : 查询嵌套字符串数组
mongodb : Query array of nested string
我有一个类似
的对象列表
[
{ 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"] } ] }
]
如何查询"type"包含hello
的所有仓库列表
db.inventory.find( { "instock": { $elemMatch: {type :["hello"]}}} , {_id:0})
returns只有hello的对象
Dot notation 在这种情况下有效,尝试:
db.collection.find({ "instock.type": "hello"})
我有一个类似
的对象列表[
{ 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"] } ] }
]
如何查询"type"包含hello
的所有仓库列表db.inventory.find( { "instock": { $elemMatch: {type :["hello"]}}} , {_id:0})
returns只有hello的对象
Dot notation 在这种情况下有效,尝试:
db.collection.find({ "instock.type": "hello"})