Hello everyone I trying to find particular data using a filter but I don't know how we set or use a filter and it properties.I have stored score 1 for true and 0 for false. And I want when next time I call the question it filters the question on scores. So that 0th id question will be displayed on browser and not one
1).This is my schema where I store question id, score and time for every single question
child:{
quiz:
questionId:{type:String},
score:{type:Number},
time:{type:String}
}
}
2). This is question schema
_id:{type:String},
question:{type:String},
answer:{type:String}
3). This is my node js code use to fetch and set filter Actually I not getting the idea how we set filter so that next time I call the API only 0th score id question will appear on my browser, not 1
var childinfo = require('../models/child.js');
var childquestion = require('../models/question.js');
this.filter = function(req, res, next){
async.waterfall({
function(callback){
try{
var query = {child.quiz.score:1};
var projection = '';
childinfo.find(query,function(err,data){
if(err) return next(err);
callback(null, data)
});
}
catch(err){
console.log(err);
return next(err);
}
},
function(callback, data){
try{
var childq = new childquestion();
var query = {data.child.quiz.questionId === childq._id};
var projection = '';
childquestion.filter(query,projection)
.skip()
.exec(function(err,data){
if (err) return next(err);
res.send(data);
});
}
catch(err){
console.log('Error While Saving the result ' +err);
return next(err);
}
}
});
}
Queries on nested field in mongodb can be written like this:
childinfo.find({"child.quiz.score": 1},function(err,data){ .. })
You should quote the fields and use the : instead of ==. So your code should be:
var childinfo = require('../models/child.js');
var childquestion = require('../models/question.js');
this.filter = function(req, res, next){
async.waterfall({
function(callback){
try{
var query = { "child.quiz.score" : 1 };
var projection = '';
childinfo.find(query,function(err,data){
if(err) return next(err);
callback(null, data)
});
}
catch(err){
console.log(err);
return next(err);
}
},
function(callback, data){
try{
var childq = new childquestion();
var query = {"data.child.quiz.questionId":childq._id};
var projection = '';
childquestion.find(query,projection)
.skip()
.exec(function(err,data){
if (err) return next(err);
res.send(data);
});
}
catch(err){
console.log('Error While Saving the result ' +err);
return next(err);
}
}
});
}
edit: code-edit
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.