I have two databases.
- One database with user information.
- One database with sales information.
I am trying to aggregate on a collection. I need to do a $lookup with the localField being an user ID in the user database.
How can I do this?
In mongoose I can use populate and specify the model I want to use, like this:
.populate({
path: 'user',
select: 'name',
model: UserModelFromDifferentDB
})
But in the $lookup I can't see such an option?
Example where the issue starts:
const data = await Sale.aggregate([
{
$match: {
creator: Types.ObjectId("5d5154a37dcf4a00171c0018")
}
},
{
$lookup:
{
from: 'users', // no users collection in this DB, it is in the other db
localField: 'user',
foreignField: '_id',
as: 'user'
}
},
])