findById: async (db, id) => {
return db.aggregate([
{
$lookup: {
from: "collections",
let: { jobId: "$_id" },
pipeline: [
{ $addFields: { productId: { $toObjectId: "$goodsid" } } },
{ $match: { $expr: { $eq: ["$productId", "$$jobId"] } } },
],
as: "product_collection",
},
},
{ $match: { $expr: { $eq: [{ $toString: "$_id" }, id] } } },
]);
},
根据从表过滤主表数据
findCollectByUid: async (db, uid) => {
return db.aggregate([
{
$lookup: {
from: "collections",
let: { jobId: "$_id" },
pipeline: [
{ $addFields: { ProductId: { $toObjectId: "$goodsid" } } },
{ $match: { $expr: { $eq: ["$ProductId", "$$jobId"] }, uid: uid } },管道内过滤
],
as: "collect",
},
},
{ $match: { "collect.0.uid": uid } }, // {$match:{"主表字段":"条件","collect.从表字段":"条件"} }])
]);
},
网友评论