0
In my MongoDB, I have these documents
- Topic, 2. MCQS, 3. True/false The topic has different questions like MCQs and true/false and in the MCQs and true/false table the Id of the topic is saved as a foreign key and in the topic table, I have userId saved as a foreign key so that I can get the topics created by a specific user. Now I want to make an API that can get data from these two documents(MCQs, t/f) on the basis of topicId. Can anyone please help me, I am creating the backend for the first time. I joined these table by using lookup.
Topic.aggregate([
{ $match: { _id: new ObjectId(req.params.id) } },
{
$lookup: {
from: "mcqs",
localField: "_id",
foreignField: "topicId",
as: "topics_mcqs_info"
},
},
{
$lookup: {
from: "true_falses",
localField: "_id",
foreignField: "topicId",
as: "topics_trueFalse_info"
}
},
])
I am getting the result now I want to sort the questions by their sequence number.
This my question json object. How can I sort questions? As I am getting first all the mcqs. then true false.
{
"_id": "629f451869b9778bdd7f4b16",
"mcqs": "Hello mcqs",
"option1": "a",
"option2": "b",
"option3": "c",
"option4": "d",
"answer": "option2",
"sequence": 1,
"topicId": "629f44f969b9778bdd7f4b10"
}