【发布时间】:2021-11-26 21:36:11
【问题描述】:
我有 2 个收藏,如下所示
collectionA.insertOne({
cart: [
{ product_id: ObjectId('xxx'), quantity: 10 }
]
})
collectionB.insertMany([
{ _id: ObjectId('xxx'), name: '', price: 1 },
{ _id: ObjectId('xyz'), name: '', price: 1 },
])
当我尝试使用 foreign/localField 进行 $lookup 时,没关系
{
from: 'collectionB',
localField: 'cart.product_id',
foreignField: '_id',
as: 'product_detail'
}
但我想切换到使用如下管道:
{
from: 'collectionB',
let: {pid: '$cart.product_id'},
pipeline: [
{
$match: {
$expr: {
$eq: ['$_id', '$$pid']
}
}
}],
as: 'product_detail'
}
然后我在 product_detail 字段中收到一个空数组。 我怎样才能让它成为我的期望?
【问题讨论】: