此代码运行正常。我正在加入2个不同的文档,但是industryCategoryMapping具有一组对象行业类别。我想在对象数组中添加条件。
db.brand.aggregate([
{ $lookup:
{
from: "industryCategoryMapping",
as: 'Category',
let: { entityId: { $toString : '$_id' }},
pipeline: [
{
$match: {
$expr: {
$and: [
{ $eq: ['$type', 'BRAND'] },
{ $eq: ['$$entityId', '$entityId'] },
]
}
}
}
]
}
}, {
$project: {
name: 1, industryCategoryMapping: {
$map:
{
input: "$Category",
as: "each",
in: '$$each.industryCategories'
}
}
}
},
])
并返回结果
"_id" : ObjectId("5dfb18a6dfb32f87deeffa13"),
"name" : "Gatorade",
"industryCategoryMapping" : [
[
{
"_id" : ObjectId("5dd26e6e34d10718f843caa9"),
"isDefault" : true,
"industryCategory" : {
"_id" : ObjectId("5def644605ea8b5a7c97b48c"),
"industryCategory" : {
"_id" : ObjectId("5def644605ea8b5a7c97b48f")
}
}
},
{
"_id" : ObjectId("5dfb1514dfb32f87deeff97f"),
"isDefault" : false,
"industryCategory" : {
"_id" : ObjectId("5df8990c4d9c000b9c87592a"),
"industryCategory" : {
"_id" : ObjectId("5df8990c4d9c000b9c87592b")
}
}
}
]
]
}
但是在给定的Json结果中,industryCategoryMapping数组有2个对象,我只需要isDefault:true类型的object。谁能帮助我忽略isDefault:给定数组中的false对象。