I have three different collections. Examples below.
Collection 1
{
_id: ObjectId('123'),
studentnumber: 123,
current_position: 'A',
schoolId: ObjectId('387')
}
Collection 2
{
_id: ObjectId('456'),
studentId: ObjectId('123'),
studentnumber: 123,
firstname: 'John',
firstname: 'Doe',
schoolId: ObjectId('543')
}
Collection 3
{
_id: ObjectId('387'),
schoolName: 'Some school'
},
{
_id: ObjectId('543'),
schoolName: 'Some other school'
},
I already have an aggregation query that looks something like this. I am completely new to MongoDB aggregation. I want to know if there is any way to use a field from a different collection in localField of $lookup.
db.collection1.aggregate([
///
$lookup: {
from: "collection2",
localField: "studentnumber",
foreignField: "studentnumber",
as: "studentnumber",
},
///
$lookup: {
from: "collection3",
localField: "schoolId",
foreignField: "_id",
as: "schoolId",
}
///
])
How can I use the schoolId from collection2 in second $lookup in localField
Current output:
{
_id: ObjectId('123'),
firstname: 'John',
firstname: 'Doe',
current_position: 'A',
school: {
_id: ObjectId('387'),
schoolName: 'Some school'
}
}
Expected output:
{
_id: ObjectId('123'),
firstname: 'John',
firstname: 'Doe',
current_position: 'A',
school: {
_id: ObjectId('543'),
schoolName: 'Some other school'
}
}
Edit: Updated the current and expected ouput. Added input document.