Hello @MaBeuLux88_xxx I have tried the pipeline way. But the questions are not sorted. this JSON object I am getting.
[
{
"_id": "62a4769c989f1ace846eaf35",
"topic": "Plants",
"language": "English",
"grade": "ELEMENTARY SCHOOL Grade 1",
"noOfQuestions": "3",
"__v": 0,
"topics_mcqs_info": [
{
"_id": "62a476f7989f1ace846eaf44",
"mcqs": "I am MCQs.",
"option1": "option1",
"option2": "option2",
"option3": "option3",
"option4": "option4",
"answer": "option2",
"sequence": 3,
"topicId": "62a4769c989f1ace846eaf35",
"__v": 0
}
],
"topics_trueFalse_info": [
{
"_id": "62a476b9989f1ace846eaf3b",
"question": "I am true false.",
"answer": "true",
"sequence": 1,
"topicId": "62a4769c989f1ace846eaf35",
"__v": 0
}
],
"topics_openEnded_info": [
{
"_id": "62a476cb989f1ace846eaf3f",
"question": "I am short question.",
"sequence": 2,
"topicId": "62a4769c989f1ace846eaf35",
"__v": 0
}
]
}
]
As In my code the mcqs lookup is first so it is returning me mcqs first regardless of the sequence.
This is my code:
const getQuestionsByTopicId = function (req, res) {
Topic.aggregate([
{ $match: { _id: new ObjectId(req.params.id) } },
{
$lookup: {
from: "mcqs",
localField: "_id",
foreignField: "topicId",
pipeline: [ {
$sort: {
sequence: 1
}
} ],
as: "topics_mcqs_info"
},
},
{
$lookup: {
from: "true_falses",
localField: "_id",
foreignField: "topicId",
pipeline: [ {
$sort: {
sequence: 1
}
} ],
as: "topics_trueFalse_info"
}
},
{
$lookup: {
from: "open_endeds",
localField: "_id",
foreignField: "topicId",
pipeline: [ {
$sort: {
sequence: 1
}
} ],
as: "topics_openEnded_info"
}
}
])
.then((result) => {
res.status(200).send(result)
console.log(result);
})
.catch((error) => {
console.log(error);
});
}
Can you please see this for me? As I am noticing it has sorted the array in lookups, but is there a way to sort it that way to get the output I want?