I can achieve this output using this command in mongo shell:
db.people.aggregate([
{
$project: {
hobbies: {
$filter: {
input: "$hobbies",
as: "hobby",
cond: { $eq: ["$$hobby.regular", true] }
}
},
name: 1,
age: 1
}
},
{
$project: {
"hobbies.name": 1,
"hobbies.type": 1,
name: 1,
age: 1
}
}
])
As you can see, I had to use two $project operators in sequence and I think this smells bad.
Is there a way to achieve the same result with another query that does not use the same operator twice and in sequence?