db.order.insert([
{ order_id: "1", uid: 10, trade_no: "111", all_price: 100, all_num: 2 },
{ order_id: "2", uid: 7, trade_no: "222", all_price: 90, all_num: 2 },
{ order_id: "3", uid: 9, trade_no: "333", all_price: 20, all_num: 6 }
]);
db.order_item.insert([
{ order_id: "1", title: "商品鼠标1", price: 50, num: 1 },
{ order_id: "1", title: "商品鼠标2", price: 50, num: 1 },
{ order_id: "1", title: "商品鼠标3", price: 0, num: 1 },
{ order_id: "2", title: "牛奶", price: 50, num: 1 },
{ order_id: "2", title: "酸奶", price: 40, num: 1 },
{ order_id: "3", title: "矿泉水", price: 2, num: 5 },
{ order_id: "3", title: "毛巾", price: 10, num: 1 }
]);
db.order.aggregate([
{
$project:{
order_id:1,
uid:1,
trade_no:1,
all_price:1,
all_num:1
}
},
{
$match:{
all_price:{
$gte:90
}
}
},
{
$sort:{
all_price:-1
}
},
{
$limit:2
},
{
$skip:1
}
])
db.order_item.aggregate([
{
$group:{
_id:'$order_id',total:{
$sum:'$num'
}
}
},
])
db.order.aggregate([
{
$lookup:{
from:'order_item', // 要关联的表
localField:'order_id', //order表中的order_id
foreignField:'order_id', // order_item表中的order_id
as:'items'
}
}
])
网友评论