管道查询mongo
Posted cuter、
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了管道查询mongo相关的知识,希望对你有一定的参考价值。
> db.order.find()
{ "_id" : ObjectId("61284410018a34fe9f76e303"), "order_id" : "1", "uid" : 10, "trade_no" : "111", "all_price" : 100, "all_num" : 2 }
{ "_id" : ObjectId("61284418018a34fe9f76e304"), "order_id" : "2", "uid" : 7, "trade_no" : "222", "all_price" : 90, "all_num" : 2 }
{ "_id" : ObjectId("6128441f018a34fe9f76e305"), "order_id" : "3", "uid" : 9, "trade_no" : "333", "all_price" : 20, "all_num" : 6 }
> db.order_item.find()
{ "_id" : ObjectId("61284437018a34fe9f76e306"), "order_id" : "1", "title" : "商品鼠标 1", "price" : 50, "num" : 1 }
{ "_id" : ObjectId("6128443e018a34fe9f76e307"), "order_id" : "1", "title" : "商品键盘 2", "price" : 50, "num" : 1 }
{ "_id" : ObjectId("61284459018a34fe9f76e308"), "order_id" : "1", "title" : "商品键盘 3", "price" : 0, "num" : 1 }
{ "_id" : ObjectId("61284547018a34fe9f76e309"), "order_id" : "2", "title" : "牛奶", "price" : 50, "num" : 1 }
{ "_id" : ObjectId("6128454f018a34fe9f76e30a"), "order_id" : "2", "title" : "酸奶", "price" : 40, "num" : 1 }
{ "_id" : ObjectId("6128455f018a34fe9f76e30b"), "order_id" : "3", "title" : "矿泉水", "price" : 2, "num" : 5 }
{ "_id" : ObjectId("61284567018a34fe9f76e30c"), "order_id" : "3", "title" : "毛巾", "price" : 10, "num" : 1 }
> db.order.find({},{"order_id":1,"price":1})
{ "_id" : ObjectId("61284410018a34fe9f76e303"), "order_id" : "1" }
{ "_id" : ObjectId("61284418018a34fe9f76e304"), "order_id" : "2" }
{ "_id" : ObjectId("6128441f018a34fe9f76e305"), "order_id" : "3" }
> db.order.aggregate([
... {
... $project:{order_id:1,trade_no:1,all_price:1}
... }
... ])
{ "_id" : ObjectId("61284410018a34fe9f76e303"), "order_id" : "1", "trade_no" : "111", "all_price" : 100 }
{ "_id" : ObjectId("61284418018a34fe9f76e304"), "order_id" : "2", "trade_no" : "222", "all_price" : 90 }
{ "_id" : ObjectId("6128441f018a34fe9f76e305"), "order_id" : "3", "trade_no" : "333", "all_price" : 20 }
> db.order.aggregate([ { $project:{order_id:1,trade_no:1,all_price:1} }, { $match:{"all_price":{$gte:90}} } ])
{ "_id" : ObjectId("61284410018a34fe9f76e303"), "order_id" : "1", "trade_no" : "111", "all_price" : 100 }
{ "_id" : ObjectId("61284418018a34fe9f76e304"), "order_id" : "2", "trade_no" : "222", "all_price" : 90 }
> db.order_item.aggregate(
... [
... {
... $group:{_id:"$order_id",total:{$sum:"$num"}}
... }
... ]
... )
{ "_id" : "2", "total" : 2 }
{ "_id" : "1", "total" : 3 }
{ "_id" : "3", "total" : 6 }
db.order.aggregate([
... {
... $project:{order_id:1,trade_no:1,all_price:1}
... },
... {
... $match:{"all_price":{$gte:90}}
... },
... {
... $sort:{"all_price":-1}
... }
... ])
{ "_id" : ObjectId("61284410018a34fe9f76e303"), "order_id" : "1", "trade_no" : "111", "all_price" : 100 }
{ "_id" : ObjectId("61284418018a34fe9f76e304"), "order_id" : "2", "trade_no" : "222", "all_price" : 90 }
关联查询
db.order.aggregate([
{ $lookup:
{
from: "order_item",
localField: "order_id",
foreignField: "order_id",
as: "items"
}
}
]
)
{
"_id": ObjectId("5b743d8c2c327f8d1b360540"),
"order_id": "1",
"uid": 10,
"trade_no": "111",
"all_price": 100,
"all_num": 2,
"items": [{
"_id": ObjectId("5b743d9c2c327f8d1b360543"),
"order_id": "1",
"title": "商品鼠标 1",
"price": 50,
"num": 1
}, {
"_id": ObjectId("5b743da12c327f8d1b360544"),
"order_id": "1",
"title": "商品键盘 2",
"price": 50,
"num": 1
}, {
"_id": ObjectId("5b74f457089f78dc8f0a4f3b"),
"order_id": "1",
"title": "商品键盘 3",
"price": 0,
"num": 1
}]
} {
"_id": ObjectId("5b743d902c327f8d1b360541"),
"order_id": "2",
"uid": 7,
"trade_no": "222",
"all_price": 90,
"all_num": 2,
"items": [{
"_id": ObjectId("5b743da52c327f8d1b360545"),
"order_id": "2",
"title": "牛奶",
"price": 50,
"num": 1
}, {
"_id": ObjectId("5b743da92c327f8d1b360546"),
"order_id": "2",
"title": "酸奶",
"price": 40,
"num": 1
}]
} {
"_id": ObjectId("5b743d962c327f8d1b360542"),
"order_id": "3",
"uid": 9,
"trade_no": "333",
"all_price": 20,
"all_num": 6,
"items": [{
"_id": ObjectId("5b743dad2c327f8d1b360547"),
"order_id": "3",
"title": "矿泉水",
"price": 2,
"num": 5
}, {
"_id": ObjectId("5b743dff2c327f8d1b360548"),
"order_id": "3",
"title": "毛巾",
"price": 10,
"num": 1
}]
}
以上是关于管道查询mongo的主要内容,如果未能解决你的问题,请参考以下文章