I am using mongo v3.4.4
How do I get the object of an array.
Collection sample
{
name: earth
age: 12
cars : [
{
brand : tesla
color : red
time : 123
},
{
brand : toyota
color : black
time : 124
},
]
},
{
name: mars
age: 15
cars : [
{
brand : volvo
color : green
time : 125
},
{
brand : honda
color : blue
time : 126
},
]
}
What I need is only:
{
brand : tesla
color : red
time : 123
}
I have tried:
db.users.aggregate([
{
$match:{"name":"earth"}
},
{
$project:
{"cars":
{"$filter":
{
input:"$cars",
as:"cars",
condition: "{$eq:[ $$cars.brand :"tesla"]}"
}
}
}
}
])
However, I still don't see the output that I expected. I am not sure if I am not using the filters correctly. If there is only one object in the cars array, then I would only return that one object. I have looked at this example