Answer the question
In order to leave comments, you need to log in
How to filter an array at each array element of a MongoDB collection?
There is a collection:
[
{
"name": "Alex",
"cars": [
{
"label": "BMW",
"age": 13,
"things": [ ... ]
},
{
"label": "Mercedes",
"age": 8,
"things": [ ... ]
}
]
},
{ ... }
]
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 ]
}
}
}
}
}
}
}
}
])
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question