https://mongoplayground.net/p/_KeECXkJTfB ,这里我们在查找之前已展开产品,然后比较找到的类别,然后如果找到则将类别设置为 true。
db.product.aggregate([
{
"$unwind": "$products"
},{
"$lookup": {
"from": "category","localField": "products.productId","foreignField": "_id","as": "inventory_docs"
}
},{
"$unwind": {
path: "$inventory_docs",preserveNullAndEmptyArrays: true
}
},{
"$addFields": {
"products.category": {
$cond: {
if: {
"$gt": [
{
$strLenCP: {
"$toString": {
"$ifNull": [
"$inventory_docs.category",""
]
}
}
},0
]
},then: true,else: false
}
}
}
}
])
本文链接:https://www.f2er.com/52984.html