3

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); 
            }
        } 
       });
     } 

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

I added the complete example.
{"message":"Object function model(doc, fields, skipId) {\n if (!(this instanceof model)) {\n return new model(doc, fields, skipId);\n }\n Model.call(this, doc, fields, skipId);\n } has no method 'filter'"}
Change .filter with .find
it GET with empty array [ ] in my browser

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.