mongodb aggregation聚合管道 $lookup结合$match实现筛选
很多老前辈说mongodb没法实现关联查询。最新的mongodb有方法了
Mongodb Mongoose aggregation 视频教程: https://pan.baidu.com/s/1Dva3FBLYILWSPubHy8kOFA
mongodb aggregation文档地址: https://docs.mongodb.com/v3.2/reference/operator/aggregation/lookup/index.html
mongodb aggregation聚合管道 $lookup结合$match实现筛选
db.orders.aggregate([
{
$unwind: "$specs"
},
{
$lookup:
{
from: "inventory",
localField: "specs",
foreignField: "size",
as: "inventory_docs"
}
},
{
$match: { "inventory_docs": { $ne: [] } }
}
])
1 回复
如果要筛选 inventory_docs中的字段的话就按照上面的写法
返回指定字段:
db.order.aggregate([
{
$lookup:
{
from: "order_item",
localField: "order_id",
foreignField: "order_id",
as: "items"
}
},
{
$match:{"all_price":{$gte:90}}
},{
$project:{order_id:1,uid:1,trade_no:1,all_price:1,all_num:1,cate:{ items:1,price:1}}
}
])