Anda dapat menggunakan $redact
operator:
db.buildings.aggregate([
{$match:{"zone.AHU":{$exists:true}}},
{$redact:{
$cond:{
if:{$or:[{$eq:["$AHU","C"]},{$not: "$AHU"}]},
then:"$$DESCEND",
else:"$$PRUNE"
}
}}
])
Ingat {$not: "$AHU"}
penting untuk dimasukkan agar elemen teratas tidak dikecualikan. Jika tidak ditambahkan, elemen teratas akan dilewati dan karenanya melewatkan seluruh dokumen yang disematkan.
Keluaran:
{
"_id" : ObjectId("5aba4460a042dc4a2fdf26cd"),
"name" : "Test Street",
"coordinate" : [
12,
31
],
"yearlyEnergyCost" : 1444,
"zone" : [
{
"name" : "AHU-C-Z2",
"_id" : ObjectId("5aba4460a042dc4a2fdf26ce"),
"AHU" : "C",
"precooling" : [],
"subZone" : []
},
{
"name" : "AHU-C-Z1",
"AHU" : "C",
"_id" : ObjectId("5ac09c898249affa03506eff"),
"precooling" : [],
"subZone" : []
},
{
"name" : "AHU-C-Z3",
"AHU" : "C",
"_id" : ObjectId("5ac09c898249affa03506efe"),
"precooling" : [],
"subZone" : []
}
],
"__v" : 2
}