Debe tener al menos MongoDB v3.4.4
Proyectando "array.a": 1
devolverá ["A", "A", "A"]
.Desde su array
el campo es una matriz, necesitamos usar $map
operador de iteración.
Para iterar sobre claves de objeto, debemos transformarlo en matriz con $objectToArray operador.
"c": { "c": [
"x": true, {k: "x", v: true},
"y": true, -> {k: "y", v: true},
"z": false {k: "z", v: false},
} ]
Luego, aplicamos $filter
operador para obtener solo el k:v
pares cuyo v
es true
.
db.collection.aggregate([
{
$match: {}
},
{
$project: {
array: {
$map: {
input: "$array",
as: "arr",
in: {
a: "$$arr.a",
b: "$$arr.b",
c: {
$size: {
$filter: {
input: {
$objectToArray: {
$ifNull: [
"$$arr.c",
{}
]
}
},
cond: {
$eq: [
"$$this.v",
true
]
}
}
}
}
}
}
}
}
}
])