I have a collection:
[
{
"name": "Alex",
"cars": [
{
"label": "BMW",
"age": 13,
"things": [ ... ]
},
{
"label": "Mercedes",
"age": 8,
"things": [ ... ]
}
]
},
{ ... }
]
I would to filter out things
s of each car
of each person. So, get the same collection (with the same schema) but without some of thing
s.
Ho do I do this via MongoDB aggregations?
UPD: The "possible duplicate" explains how to filter subarray but not subsubarray in it (AFAIK).
UPD2: I wrote some code that filter subsubarray but I should point out all fields I want to save...
db.matches.aggregate([{
$project: {
'_id': '$_id',
'cars': {
$map: {
input: '$cars',
as: 'car',
in: {
'things': {
$filter: {
input: '$$car.things',
as: 'thing',
cond: {
$lte: [ '$$thing.cost', 10 ]
}
}
}
}
}
}
}
}
])
Question: can I automatically preserve other fields for person
s and their car
s (because there are many fields in real world)?