Du kan lägga till kapslade $cond
för att filtrera nycklarna 123 eller 456. Sedan ett sista $project
scenen kan användas för att konstruera arrayen.
db.events.aggregate([
{"$group":
{ "_id":"$KLLS",
"Processus":{"$push":{"$cond":[{"$eq":["$type","Processus"]},'$$ROOT',false]}},
"Work123":
{"$push":
{"$cond":
[
{"$eq":["$type","Work"]},
{"$cond":
[
{"$eq":["$key","123"]},
'$$ROOT',
false
]
},
false
]
}
},
"Work456":
{"$push":
{"$cond":
[
{"$eq":["$type","Work"]},
{"$cond":
[
{"$eq":["$key","456"]},
'$$ROOT',
false
]
},
false
]
}
},
"Viewing":{"$push":{"$cond":[{"$eq":["$type","Viewing"]},'$$ROOT',false]}}
}
},
{"$project": { "_id":0, "KLLS":"$_id", "Processus":{"$setDifference":["$Processus",[false]]},
"123":{"$setDifference":["$Work123",[false]]},
"456":{"$setDifference":["$Work456",[false]]},
"Viewing":{"$setDifference":["$Viewing",[false]]}
}
},
{"$project": { "KLLS":1, "Processus":1, "Work" : [{"123" : "$123"}, {"456" : "$456"}],"Viewing":1}}
])