How to find particular data using a filter in mongodb using node js

Published on Author Code Father

Below is the example, How to find particular data using a filter in mongodb using node js?

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

Comments

comments