var questionSchema = new Schema({
category: { name:
{
type: String,
lowercase: true,
required: [true, 'Category is a required field']
},
question:[
{
q: {
type: String,
lowercase: true
},
options: {
option1:{
type: String,
lowercase: true
},
option2:{
type: String,
lowercase: true
]
},
option3:{
type: String,
lowercase: true
},
option4:{
type: String,
lowercase: true
},
},
ans: {
type: String,
lowercase: true
},
level:{
type: String,
lowercase: true
}
}
]
},
},
{
strict: true,
runSettersOnQuery: true,
timestamps: {
createdAt: 'created', updatedAt: 'updated'
}
});
function (req,res,next) {
console.log(req.params.qcategory);
Question.find({
category:{
name: req.params.qcategory,
}
},'', function (err,data) {
if (err) {
err.status = 406;
return next(err);
}
console.log(data);
return res.status(201).json({
message: ' success.',data:data
})
})
};
Try this, Not tested
function (req,res,next) {
console.log(req.params.qcategory);
Question.find({
"category.name": req.params.qcategory,}
, function (err,data) {
if (err) {
err.status = 406;
return next(err);
}
console.log(data);
return res.status(201).json({
message: ' success.',data:data
})
})
};
Replace the {category:{name:req.params.qcategory}} with {"category.name":req.params.qcategory}.
read more on mongodb's Query Embedded Documents https://docs.mongodb.com/.../method/db.collection.find/