I am creating a questionnaire that has the following schema:
var questionnaireSchema = new Schema({
user_id : String,
start_date : Date,
end_date : Date,
questions : [{
question_id : {type: Number},
answer_id : {type: Number},
answer_string : String
}]
});
I need to be and to find an answer set by the user_id and then see if that user has answered that question already.
I have the following query:
Questionnaire.findOne({'user_id': req.body.uid}, function (err, user) {
if (err) {
throw err;
}
if (user) {
Questionnaire.findOne({
'uid': req.body.uid,
'questions.question_id': req.body.question_id
}, function (err, question) {
if (question) {
console.log('Found a question - updating');
} else {
console.log('No question found - adding');
}
});
}
});
The query finds the user, however, when I try to find the user and question.answer_id together, the query never find anything.
I'm not sure how to write this query or where to look.
Thank you
Assesmentmodel? Shouldn't the query be onQuestionnairetoo, and reduced the code to a single query?uidkey in theQuestionnairemodel?