[英]filter in mongodb using node js
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.大家好,我尝试使用过滤器查找特定数据,但我不知道我们如何设置或使用过滤器及其属性。我已将 1 分表示为真,0 分表示为假。 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
这样第 0 个 id 问题将显示在浏览器上,而不是一个
1).This is my schema where I store question id, score and time for every single question 1). 这是我的架构,我在其中存储每个问题的问题 ID、分数和时间
child:{
quiz:
questionId:{type:String},
score:{type:Number},
time:{type:String}
}
}
2). 2)。 This is question schema
这是问题架构
_id:{type:String},
question:{type:String},
answer:{type:String}
3). 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
这是我用于获取和设置过滤器的节点 js 代码实际上我不知道我们如何设置过滤器,这样下次我调用 API 时,我的浏览器上只会出现第 0 个分数 id 问题,而不是 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: mongodb中嵌套字段的查询可以这样写:
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编辑:代码编辑
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.