In the 'QAns' model, I want to load all the documents in which the 'Answers' field is an empty array using mongoose in node.js. I am using MongoDB Atlas for storing my data. This is my schema for the database:
const QuestAnsSchema = new Schema({
text: String,
author: String,
answers: [{
text: String,
author: String
}]
});
This is the data in the MongoDB database.
{
"_id":{"$oid":"5ee1f235a1e9870daca7d5e9"},
"text":"Question 1",
"author":"5ee1b8ebdbf91b23a808d417",
"answers":[],
"__v":{"$numberInt":"0"}
},
{
"_id":{"$oid":"5ee1f235885770darrr7f449"},
"text":"Question 2",
"author":"5ee1b8ebdbf9o2w3a808d417",
"answers":[],
"__v":{"$numberInt":"0"}
}
Both the documents have the 'answers' field empty in them but assuming there would be some documents with a non-empty 'answers' field how will I load the ones with no answers field?
I have tried this code but it gives a 'Bad Request' error:
router.get('/questions', (req, res) => {
QAns.find({answers: { $exists: true, $ne: [] } }).then(( err, result) => {
if(err) return res.sendStatus(400);
res.render('questions',{ result });
console.log(result);
});
})
- QAns is the model name, answers is the array field
- 'questions' is the ejs file which will display those documents after they are loaded.