我是MongoDB和Express JS的新手
我有一个产品型号看起来像
const productSchema = mongoose.Schema({ name:String, 标题:字符串, brand_id:{type:mongoose.Schema.Types ….
MongoDB 3.6或更高版本有新功能 抬头 句法
db.orders.aggregate([{ $lookup: { from: "products", let: { "productId": "$product_id", "productVarientId": "$product_varient_id" }, pipeline: [ { $match: { $expr: { $eq: [ "$_id", "$$productId" ]} } }, { $addFields: { varient: { $filter: { input: "$varient", as: "varient", cond: { $eq: [ "$$productVarientId", "$$varient._id" ] } } } } } ], as: "product_data" } }])
您可以检查数据样本 这里