/**
Assume we have docs in MyCollection with the folowing structure:
{
"users": [
{"name": "John", "age": 34},
{"name": "Kate", "age": 17},
{"name": "Simon", "age": 65},
{"name": "Alice", "age": 27}
]
}
*/
db.MyCollection.aggregate([
{
"$project": {
"users": {
"$filter": {
"input": "$users",
"as": "user",
"cond": {
"$and": [
{"$gte": ["$$user.age", 18]},
{"$lte": ["$$user.age", 50]}
]
}
}
}
}
}
])
/**
This way we'll have the result
{
"_id" : ObjectId("63690f75ccdacb91c24682f0"),
"users" : [
{
"name" : "John",
"age" : 34
},
{
"name" : "Alice",
"age" : 27
}
]
}
*/