Py学习  »  MongoDB

mongodb:嵌套字符串的查询数组

Mohammed Atif • 5 年前 • 635 次点击  

我有一个物品清单

[
   { 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})

返回只有hello的对象。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/30817
 
635 次点击  
文章 [ 1 ]  |  最新文章 5 年前
mickl
Reply   •   1 楼
mickl    5 年前

Dot notation

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

MongoDB playground