mongodb - mongodb 2级聚合
问题描述
我有 3 个相互引用的集合,这样
A -> B -> C
我想在与 B 匹配并从 B 到 C 后过滤数据
集合 1
Products: [{
_id:ObjectId(),
name:"product1",
productCatalogue:[reference to productCatalogue collection]
},....]
收藏 2
productCatalogue: [{
_id:ObjectId(),
name:"catelgoue1",
category:{
cat:[reference to category table],
sub1:[reference to category table],
sub2:[reference to category table]
}
},...]
集合 3
category: [{
_id:ObjectId(),
name:"cat1",
type:"parent"
},....]
我想过滤数据,以便使用聚合过滤具有目录:catelgoue1 和类别:cat1 的产品。
解决方案
您可以在每次查找 $match 示例代码之前使用:
const orderProcess = await Order.aggregate([
{
$match: {
userId: userId
}
},
{
$lookup: {
from: "products",
localField: "product_id",
foreignField: "_id",
as: "product",
}
},
{
$unwind: "$product"
},
{
$lookup: {
from: "prices",
localField: "product_id", // $product._id => like top lookup data
foreignField: "productId", // $product._id => like top lookup data
as: "price"
},
},
{
$unwind: "$price"
},
{
$project:{
// response all item
}
}])
推荐阅读
- python - Python Bokeh - 使用两个同步的选择小部件过滤表
- alexa - 在特定 Alexa 设备上触发通知
- reactjs - 如何将 ref 类型的道具与诸如 Popover 之类的 Meterial-ui 组件一起使用?试图了解如何使用 'action'= updatePosition() 道具
- android - roboelectric 中工作经理的问题
- java - 在localhost启动tomcat v9.0服务器遇到问题
- database - SnowFlake 在 group by vs partition on vs distinct 上的表现
- java - 使用 JpaRespository 时如何检查 saveAndFlush() 是否成功更新了数据库中的实体参数?
- reactjs - 部署时反应onClick函数未执行
- nginx - Nginx -- 如果是referer,将(x-forwarded-for) ip 加入黑名单
- performance - Gatling 油门未达到每秒配置的请求数