Here i have Two arrays,
Usercar = [{
parentId :001
cars:[
{_id: 1, name: bmw, color: red},
{_id: 2, name: Ford, color: black},
{_id: 3, name: Volkswagen, color: black},
]
}]
Userfavorite =
[{
parentId :001,
favoriteCars:[1,3] //mongoose.Types.ObjectId
}]
I want o show user favorite cars using mongodb aggregate, here is my code
let carsId= [1,3];
{$match: {
parentId :001
}},
{
$project:{
cars:{
$filter:{
input:"$cars",
as :'cars',
cond:{ $eq :["$$cars._id", mongoose.Types.ObjectId('1')]}
//cond:{ $eq :["$$cars._id", carsId]}
}
}
}
}
the above code only work, when pass single carsId, I want user favorite Cars details from Usercar's collection, how to do that in mongodb aggregate ?
UsercarandUserfavoritein the same document, or separate collections?