Je kunt het proberen,
$facet
om 2 arrays te maken,users
gebruikersgegevens naam en studentId, ten tweede alle gebruikersgegevens inallUsers
$project
herhaal lus$map
invoer als allUsers-array$map
invoer als studentenReffered array$map
invoer als leerlingenarray$reduce
om gegevens van de leerling te krijgen vanusers
array wanneer voorwaarde overeenkomt
$unwind
deconstrueer allUsers-array$replaceWith
vervang allUsers-object in root
db.collection.aggregate([
{
$facet: {
users: [
{
$project: {
studentId: 1,
name: 1
// add fields as you want it will automatically reflect in join
}
}
],
allUsers: []
}
},
{
$project: {
allUsers: {
$map: {
input: "$allUsers",
in: {
$mergeObjects: [
"$$this",
{
studentsReffered: {
$map: {
input: "$$this.studentsReffered",
in: {
$mergeObjects: [
"$$this",
{
students: {
$map: {
input: "$$this.students",
as: "s",
in: {
$reduce: {
input: "$users",
initialValue: { studentId: "$$s.studentId" },
in: {
$cond: [
{ $eq: ["$$this.studentId", "$$s.studentId"] },
"$$this",
"$$value"
]
}
}
}
}
}
}
]
}
}
}
}
]
}
}
}
}
},
{ $unwind: "$allUsers" },
{ $replaceWith: "$allUsers" }
])