【发布时间】:2021-09-18 19:19:41
【问题描述】:
这是我的 MongoDB 数据库的两个模型。
QuestionsInfo
{
"_id": ObjectId("60e5f5fce2446faa95e6eca7"),
"courseName": "ML",
"user": ObjectId("6087dc4c2ba7a828363c9fca"),
"questions": [
{
"questionInput": {
"question": "This is the first Question. `(a+b)^2` ",
},
"id": "aLC/QNz/AOLO9Fyj7oJT",
"createdAt": "2021-07-07T18:41:18.971Z"
},
{
"questionInput": {
"question": "This is the first Question. `(a+b)^2` ",
},
"id": "aLC/QNz/AOLO9Fyj7oJJ",
"createdAt": "2021-07-07T18:41:19.971Z"
},
{
"questionInput": {
"question": "This is the third Question.ΒΓ",
},
"id": "qPgd261wVGizOuR1b9RT",
"createdAt": "2021-07-07T18:46:25.203Z"
}
]
}
ExamInfo
questionsInfo 是QuestionsInfo 模型的ID。这是由 MongoDB 引用创建的。
{
"_id": ObjectId("60e5f88159292f575c0ca17f"),
"questionsId": [
"aLC/QNz/AOLO9Fyj7oJT",
"aLC/QNz/AOLO9Fyj7oJJ"
],
"user": ObjectId("6087dc4c2ba7a828363c9fca"),
"questionsInfo": ObjectId("60e5f5fce2446faa95e6eca7"),
}
我想使用填充到ExamInfo 模型中查找来自QuestionsInfo 与questionsId 列表匹配的问题。
我该怎么做?提前致谢。
【问题讨论】:
-
您是否引用了您的模型。您可以为此使用
.populate('questionsInfo')。或使用$lookup -
我使用了填充但无法返回完全匹配的问题。
-
您的要求比我在
questionsInfo文档中填充 ID 存在于questionsId中的问题。为此,您需要$lookup -
你能写出查询吗?我不明白。
标签: mongodb mongoose aggregation-framework mongoose-populate