在数组对象中获取查找结果,并用查找结果替换数组中对象的值

时间:2019-02-28 09:42:31

标签: node.js mongodb mongoose

我将ObjectId放在名为questionId的数组中。当我将lookup的{​​{1}}用作questionId时,它将生成一个名为questionId的新数组,并将所有扩展结果推入其中。我需要将数组中的questionId替换为相应questionId的查找结果,就像questionId一样。

这就是我的查询内容

populate

哪个抓取

Attempt.aggregate([{
      $match: {
        _id: ObjectId(attemptId)
      }
    },
    {
      $project: {
        responseMCQ: 1,
        responseNumeric: 1
      }
    },
    {
      $lookup: {
        from: 'mcqs', localField: 'responseMCQ.questionId', foreignField: '_id', as: 'mcqQuestion.questionId'
      }
    },
    {
      $lookup: {
        from: 'numericquestions', localField: 'responseNumeric.questionId', foreignField: '_id', as: 'numericQuestions'
      }
    },

最终结果应该类似于:

[
    {
        "_id": "5c73e49572f67e1323ad4109",
        "responseMCQ": [
            {
                "marks": -1,
                "responseTime": 1,
                "options": [
                    "5c70f593280c4111fcddde4d"
                ],
                "isCorrect": false,
                "_id": "5c73e4a172f67e1323ad410b",
                "questionId": "5c70f593280c4111fcddde4c"
            }],

        "mcqQuestion": {
            "questionId": [
                {
                    "_id": "5c70f593280c4111fcddde4c",
                    "difficulty": 1,
                    "reviewer": null,
                    "deletedAt": null,
                    "isMultiple": false,
                    "isPassage": false,
                    "body": "<p>dsad</p>",
                    "options": [
                        {
                            "correct": false,
                            "_id": "5c70f593280c4111fcddde50",
                            "value": "<p>asdsd</p>"
                        },
                        {
                            "correct": true,
                            "_id": "5c70f593280c4111fcddde4f",
                            "value": "<p>asdd</p>"
                        },
                        {
                            "correct": true,
                            "_id": "5c70f593280c4111fcddde4e",
                            "value": "<p>asdasd</p>"
                        },
                        {
                            "correct": false,
                            "_id": "5c70f593280c4111fcddde4d",
                            "value": "<p>sadasd</p>"
                        }
                    ],
                    "solution": "<p>asdd</p>",
                    "chapterId": "5c53fdf3ffc0703d83f6eb5d",
                    "subjectId": "5c53fdc9ffc0703d83f6eb5a",
                    "orgId": "5c53fce31b199606c9113b1b",
                    "creatorId": "5c53fd42298ec304ba5e8d84",
                    "created_at": "2019-02-23T07:26:11.961Z",
                    "updated_at": "2019-02-23T07:26:11.961Z",
                    "__v": 0,
                    "subtopics": []
                } ]
        }
    }
]

在哪里,将ChapterId添加到[ { "_id": "5c73e49572f67e1323ad4109", "responseMCQ": [ { "marks": -1, "responseTime": 1, "options": [ "5c70f593280c4111fcddde4d" ], "isCorrect": false, "_id": "5c73e4a172f67e1323ad410b", "questionId": "5c70f593280c4111fcddde4c", "chapterId": "5c53fdf3ffc0703d83f6eb5d" } ], "mcqQuestion": { "questionId": [ { "_id": "5c70f593280c4111fcddde4c", "difficulty": 1, "reviewer": null, "deletedAt": null, "isMultiple": false, "isPassage": false, "body": "<p>dsad</p>", "options": [ { "correct": false, "_id": "5c70f593280c4111fcddde50", "value": "<p>asdsd</p>" }, { "correct": true, "_id": "5c70f593280c4111fcddde4f", "value": "<p>asdd</p>" }, { "correct": true, "_id": "5c70f593280c4111fcddde4e", "value": "<p>asdasd</p>" }, { "correct": false, "_id": "5c70f593280c4111fcddde4d", "value": "<p>sadasd</p>" } ], "solution": "<p>asdd</p>", "chapterId": "5c53fdf3ffc0703d83f6eb5d", "subjectId": "5c53fdc9ffc0703d83f6eb5a", "orgId": "5c53fce31b199606c9113b1b", "creatorId": "5c53fd42298ec304ba5e8d84", "created_at": "2019-02-23T07:26:11.961Z", "updated_at": "2019-02-23T07:26:11.961Z", "__v": 0, "subtopics": [] } ] } } ] 中,而不是创建一个单独的数组然后获取它。

0 个答案:

没有答案