As you know, mongodb is a document oriented database, so I don't have relationship between 2 objects. With mongoose, it's have a function called populate to JOIN 2 collection with each other, so we have to use populate or $lookup to get data from other collection with a reference field.
But which way is faster ?
Sample:
Users: {
_id: ObjectId,
refId: {
type: ObjectId,
ref: 'Comments'
}
}
db.Users.find().populate({ path: 'refId' });
or
db.User.aggregate([
{
$lookup: {
from: 'Comments',
localFields: 'refId',
foreignField: '_id',
as: 'comments'
}
}
])