MongoDB Query Array of Embedded

[
   { item: "journal", instock: [ { warehouse: "A", qty: 5 }, { warehouse: "C", qty: 15 } ] }, # 1
   { item: "notebook", instock: [ { warehouse: "C", qty: 5 } ] }, # 2
   { item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 15 } ] }, # 3
   { item: "planner", instock: [ { warehouse: "A", qty: 40 }, { warehouse: "B", qty: 5 } ] }, # 4
   { item: "postcard", instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] } # 5
]

1.查询instock节点序列中含有{ warehouse: "A", qty: 5 }的doc
{ "instock": { warehouse: "A", qty: 5 } } ->1
2.查询instock节点序列中元素含有qty元素小于20的doc .序列节点可以直接跨序列访问序列元素
{ 'instock.qty': { $lte: 20 } } ->1,2,3,4,5
3.查询instock节点序列第0个元素的qty字段小于等于20的doc
{ 'instock.0.qty': { $lte: 20 } }->1,2,5
4.$elemMatch表示至少有一个节点符合条件
{ "instock": { $elemMatch: { qty: 5, warehouse: "A" } } }
5.
{ "instock": { $elemMatch: { qty: { $gt: 10, $lte: 20 } } } }
6.序列中有大于10的或者小于20的doc
{ "instock.qty": { $gt: 10, $lte: 20 } } ->1,3,4,5
7.两个条件同时满足
{ "instock.qty": 5, "instock.warehouse": "A" } ->1,4

参考:
https://docs.mongodb.com/manual/tutorial/query-documents/

你可能感兴趣的:(mongodb,mongodb)