Reputation: 49
I am trying to find and display all questions from the "Question Collection" that are linked to all the surveys created by a user I find in the "Surveys Collection", I am able to find questions for a specific survey in the list, but I want to be able to find all questions linked to different surveys.
This is what I have so far, in this case I am only finding questions specific to one survey using _id:
// Display list of user surveys
module.exports.displayUserSurveys = (req, res, next) => {
let id = req.user._id;
Survey.find({ User: id }, (err, surveyList) => {
if (err) {
console.log(err);
res.end(err);
} else {
let questionId = surveyList[0].Questions;
Question.find({ _id: questionId }, (err, questionList) => {
if (err) {
return console.error(err);
} else {
let currentDate = new Date();
res.render("content/survey/my-surveys", {
title: "My Surveys",
page: "my-surveys",
username: req.user ? req.user.username : "",
SurveyList: surveyList,
QuestionList: questionList,
today: currentDate,
});
}
});
}
});
};
Survey Schema:
let surveyModel = mongoose.Schema(
{
Title: String,
Type: [String],
Questions: { type: mongoose.Schema.Types.ObjectId, ref: "questions" },
Answered: { type: Number, default: 0 }, // how many times users answered
User: { type: mongoose.Schema.Types.ObjectId, ref: "users" },
startdate: { type: Date, default: Date.now },
enddate: { type: Date, default: Date.now() + 30 * 24 * 60 * 60 * 1000 }, //30 days to milliseconds to set default end date 30 days out
},
{
collection: "surveys",
}
);
Questions Schema:
let questionModel = mongoose.Schema(
{
MC: {
startdate: Date,
enddate: Date,
QuestionText1: String,
Options1: [String],
QuestionText2: String,
Options2: [String],
QuestionText3: String,
Options3: [String],
QuestionText4: String,
Options4: [String],
QuestionText5: String,
Options5: [String],
},
TF: {
startdate: Date,
enddate: Date,
QuestionText1: String,
Options1: Boolean,
QuestionText2: String,
Options2: Boolean,
QuestionText3: String,
Options3: Boolean,
QuestionText4: String,
Options4: Boolean,
QuestionText5: String,
Options5: Boolean,
},
},
{
collection: "questions",
}
);
Would this work?
// Display list of user surveys
module.exports.displayUserSurveys = (req, res, next) => {
let id = req.user._id;
Survey.find({ User: id }, (err, surveyList) => {
if (err) {
console.log(err);
res.end(err);
} else {
Survey.aggregate([
{ $match: { User: id }},
{
$lookup : {
from: "Question",
localField: "Questions",
foreignField: "_id",
as: "questions"
}
}
]).exec((err, questionList) => {
if(err) {
console.log(err);
res.end(err);
} else {
let currentDate = new Date();
res.render("content/survey/my-surveys", {
title: "My Surveys",
page: "my-surveys",
username: req.user ? req.user.username : "",
SurveyList: surveyList,
QuestionList: questionList,
today: currentDate,
});
}
});
}
});
};
Upvotes: 1
Views: 52
Reputation: 23835
If i understand you question correctly, I think you would like to have a JOIN like this (Representation in SQL):
SELECT *
FROM Surveys
JOIN Questions ON Surveys.Questions = Questions._id
WHERE Surverys.User = { UserID }
This can be achieved in MongoDB using Aggregations with the $lookup operator.
const surveys = await Survey.aggregate([
{ $match: { User: id }},
{
$lookup : {
from: "questions",
localField: "Questions",
foreignField: "_id",
as: "questions"
}
}
]).exec()
console.log(surveys)
// [
// { Title: "bla", User: "5d8151ec96fb4f0ed5a7a03f", ..., questions:
// [
// { MC: { ... }, TF: { ... }
// ]
// },
// { Title: "bla2", User: "5d8151ec96fb4f0ed5a7a03f", ..., questions:
// [
// { MC: { ... }, TF: { ... }
// ]
// }
// ]
// get the first survey
console.log(surveys[0])
// get the first questionText
console.log(surveys[0].questions[0].MC.QuestionText1)
But as an advice: I wouldn't use two different Collections if you only have a 1-to-1 relationship between them. Just nest the Questions into the survery.
Upvotes: 1