I have three collection: user, buyer, merchant, example below:
user:{
_id
userType: enum(BUYER, MERCHANT)
}
buyer:{
_id,
userId
}
merchant:{
_id,
userId
}
I want get user and detail of it then I using aggregate with lookup to two collection ( buyer and merchant). Now I want lookup only one collection with correct userType. Have any solution. My query below:
user.aggregate([
{
$lookup: {
from: 'buyer',
as: 'buyer',
localField: '_id',
foreignField: 'userId',
},
},
{ $unwind: { path: '$buyer', preserveNullAndEmptyArrays: true } },
{
$lookup: {
from: 'merchant',
as: 'merchant',
localField: '_id',
foreignField: 'userId',
},
},
{ $unwind: { path: '$merchant', preserveNullAndEmptyArrays: true } },
])
Thanks