mongodb多層巢狀查詢
阿新 • • 發佈:2018-12-24
> db.inventory.find( { "instock": { warehouse: "A", qty: 5 } } )
{ "_id" : ObjectId("5943714f0429b8c7852eaf20"), "item" : "journal", "instock" : [ { "warehouse" : "A", "qty" : 5 }, { "warehouse" : "C", "qty" : 15 } ] }
> db.inventory.find( { } )
{ "_id" : ObjectId("5943714f0429b8c7852eaf20"), "item" : "journal", "instock" : [ { "warehouse" : "A", "qty" : 5 }, { "warehouse" : "C", "qty" : 15 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf21"), "item" : "notebook", "instock" : [ { "warehouse" : "C", "qty" : 5 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf22"), "item" : "paper", "instock" : [ { "warehouse" : "A", "qty" : 60 }, { "warehouse" : "B", "qty" : 15 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf23"), "item" : "planner", "instock" : [ { "warehouse" : "A", "qty" : 40 }, { "warehouse" : "B", "qty" : 5 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf24"), "item" : "postcard", "instock" : [ { "warehouse" : "B", "qty" : 15 }, { "warehouse" : "C", "qty" : 35 } ] }
> db.inventory.find( { "instock": { qty: 5 } } )
> db.inventory.find( { "instock.qty": 5 } )
{ "_id" : ObjectId("5943714f0429b8c7852eaf20"), "item" : "journal", "instock" : [ { "warehouse" : "A", "qty" : 5 }, { "warehouse" : "C", "qty" : 15 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf21"), "item" : "notebook", "instock" : [ { "warehouse" : "C", "qty" : 5 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf23"), "item" : "planner", "instock" : [ { "warehouse" : "A", "qty" : 40 }, { "warehouse" : "B", "qty" : 5 } ] }
> db.inventory.find( { "instock.qty": 15} )
{ "_id" : ObjectId("5943714f0429b8c7852eaf20"), "item" : "journal", "instock" : [ { "warehouse" : "A", "qty" : 5 }, { "warehouse" : "C", "qty" : 15 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf22"), "item" : "paper", "instock" : [ { "warehouse" : "A", "qty" : 60 }, { "warehouse" : "B", "qty" : 15 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf24"), "item" : "postcard", "instock" : [ { "warehouse" : "B", "qty" : 15 }, { "warehouse" : "C", "qty" : 35 } ] }
> db.inventory.find( { "instock.qty": 15,'instock.warehouse':'B'} )
{ "_id" : ObjectId("5943714f0429b8c7852eaf22"), "item" : "paper", "instock" : [ { "warehouse" : "A", "qty" : 60 }, { "warehouse" : "B", "qty" : 15 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf24"), "item" : "postcard", "instock" : [ { "warehouse" : "B", "qty" : 15 }, { "warehouse" : "C", "qty" : 35 } ] }
> db.inventory.find( { "instock": { qty: 15, warehouse: "B" } } )
> db.inventory.find( { "instock": { warehouse: "B", qty:15 } } )
{ "_id" : ObjectId("5943714f0429b8c7852eaf22"), "item" : "paper", "instock" : [ { "warehouse" : "A", "qty" : 60 }, { "warehouse" : "B", "qty" : 15 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf24"), "item" : "postcard", "instock" : [ { "warehouse" : "B", "qty" : 15 }, { "warehouse" : "C", "qty" : 35 } ] }
> db.inventory.find( { "instock": {$match:{ warehouse: "B", qty:15 }} } )
Error: error: {
"waitedMS" : NumberLong(0),
"ok" : 0,
"errmsg" : "unknown operator: $match",
"code" : 2
}
> db.inventory.find( { "instock": {$eleMatch:{ warehouse: "B", qty:15 }} } )
Error: error: {
"waitedMS" : NumberLong(0),
"ok" : 0,
"errmsg" : "unknown operator: $eleMatch",
"code" : 2
}
> db.inventory.find( { "instock": {$elemMatch:{ warehouse: "B", qty:15 }} } )
{ "_id" : ObjectId("5943714f0429b8c7852eaf22"), "item" : "paper", "instock" : [ { "warehouse" : "A", "qty" : 60 }, { "warehouse" : "B", "qty" : 15 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf24"), "item" : "postcard", "instock" : [ { "warehouse" : "B", "qty" : 15 }, { "warehouse" : "C", "qty" : 35 } ] }
> db.inventory.find( { "instock": {$elemMatch:{ warehouse: "B" }} } )
{ "_id" : ObjectId("5943714f0429b8c7852eaf22"), "item" : "paper", "instock" : [ { "warehouse" : "A", "qty" : 60 }, { "warehouse" : "B", "qty" : 15 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf23"), "item" : "planner", "instock" : [ { "warehouse" : "A", "qty" : 40 }, { "warehouse" : "B", "qty" : 5 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf24"), "item" : "postcard", "instock" : [ { "warehouse" : "B", "qty" : 15 }, { "warehouse" : "C", "qty" : 35 } ] }
> db.inventory.find( { "instock.qty": { $gt: 10, $lte: 20 } } )
{ "_id" : ObjectId("5943714f0429b8c7852eaf20"), "item" : "journal", "instock" : [ { "warehouse" : "A", "qty" : 5 }, { "warehouse" : "C", "qty" : 15 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf22"), "item" : "paper", "instock" : [ { "warehouse" : "A", "qty" : 60 }, { "warehouse" : "B", "qty" : 15 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf23"), "item" : "planner", "instock" : [ { "warehouse" : "A", "qty" : 40 }, { "warehouse" : "B", "qty" : 5 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf24"), "item" : "postcard", "instock" : [ { "warehouse" : "B", "qty" : 15 }, { "warehouse" : "C", "qty" : 35 } ] }
> db.inventory.find( { "instock": {$elemMatch:{ "qty": { $gt: 10, $lte: 20 } }} } )
{ "_id" : ObjectId("5943714f0429b8c7852eaf20"), "item" : "journal", "instock" : [ { "warehouse" : "A", "qty" : 5 }, { "warehouse" : "C", "qty" : 15 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf22"), "item" : "paper", "instock" : [ { "warehouse" : "A", "qty" : 60 }, { "warehouse" : "B", "qty" : 15 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf24"), "item" : "postcard", "instock" : [ { "warehouse" : "B", "qty" : 15 }, { "warehouse" : "C", "qty" : 35 } ] }
>