简体   繁体   English

Mongoose 将额外的集合加入到两个聚合集合中

[英]Mongoose join additional collection to two aggregated collections

This is a new requirement I need to implement in my personal project.这是我需要在我的个人项目中实现的新要求。 I successfully joined two collections using aggregate ( previous question) .我使用聚合成功加入了两个集合( 上一个问题) Now, I have a new collection that I need to join.现在,我有一个需要加入的新集合。 Please see codes below:请查看以下代码:

Student model:学生模型:

{
  fullname: { type: String, required: true },
  email: { type: String, required: true },
}

Exams model:考试模式:

{
  test: { type: String, required: false },
  top10: [
    type: {
      studentId: { type: String, required: true },
      score: { type: Number, required: false },
    }
  ]
}

Aggregated collections:聚合集合:

db.exams.aggregate([
{ $unwind: "$top10" },
{
$lookup: {
  from: "students",
  localField: "top10.studentId",
  foreignField: "_id",
  as: "students"
}
},
{ $addFields: { students: { $arrayElemAt: ["$students", 0] } } },
{
$group: {
  _id: "$_id",
  test: { $first: "$test" },
  students: {
    $push: {
      studentId: "$top10.studentId",
      score: "$top10.score",
      fullname: "$students.fullname"
    }
  }
}
}
])

Now, the 3rd collection is called Teacher .现在,第三个集合称为Teacher

Teacher model:教师模式:

{
  fullName: { type: String, required: false },
  studentId: { type: String, required: false }
}

Expected output should be:预期输出应该是:

{
 "test": "Sample Test #1",
 "students": [
        {
            "studentId": "5f22ef443f17d8235332bbbe",
            "fullname": "John Smith",
            "score": 11,
            "teacher": "Dr. Hofstadter"
            
        },
        {
            "studentId": "5f281ad0838c6885856b6c01",
            "fullname": "Erlanie Jones",
            "score": 9,
            "teacher": "Mr. Roberts"
        },
        {
            "studentId": "5f64add93dc79c0d534a51d0",
            "fullname": "Krishna Kumar",
            "score": 5,
            "teacher": "Ms. Jamestown"
        }
    ]
}

Appreciate kind of help.感谢您的帮助。 Thanks!谢谢!

You can add these 2 more pipelines before the $group pipeline,您可以在 $group 管道之前再添加这 2 个管道,

  • $lookup join collection with teacher $lookup与老师一起收藏
  • $addFields to get convert array to object $addFields将数组转换为对象
  • $group to add teacher name in students array $group在学生数组中添加教师姓名
  {
    $lookup: {
      from: "teacher",
      localField: "top10.studentId",
      foreignField: "studentId",
      as: "students.teacher"
    }
  },
  {
    $addFields: {
      "students.teacher": { $arrayElemAt: ["$students.teacher", 0] }
    }
  },
  {
    $group: {
      _id: "$_id",
      test: { $first: "$test" },
      students: {
        $push: {
          studentId: "$top10.studentId",
          score: "$top10.score",
          fullname: "$students.fullname",
          teacher: { $ifNull: ["$students.teacher.fullname", ""] }
        }
      }
    }
  }

Playground操场

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM