試すことができます
-
$facet2つの配列を作成するには、usersユーザーの詳細名とstudentId、2番目にallUsersのすべてのユーザーの詳細 -
$projectループを繰り返す-
$mapallUsers配列として入力 -
$map学生として入力参照配列 -
$map学生の配列として入力 -
$reduceusersから学生のデータを取得する 条件が一致した場合の配列
-
-
$unwindallUsers配列を分解します -
$replaceWithルート内のallUsersオブジェクトを置き換えます
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" }
])