How we remove null error encounter in referencing node js mongodb
<p>I have to use populate method for referencing my second collection. I have two collections one is levels and second child.
I have referenced to my level collection through child collection. For this, I use the populated method but it returns null.</p>

<p>This is node js code</p>

<pre><code>function(){
            var query = {'_id': result._id };
            var params = 'usercurrLevelId';
            childQuizInfo.findOne(query, params).populate({
              path : 'usercurrLevelId',
              model : 'Level',
              select : '_id level_num age'
            }).exec(function(err,levelinfo){
              if(err) return next(err);
              res.send(levelinfo);
            });
          }
</code></pre>

<p>This level schema</p>

<pre><code>var LevelSchema = new Schema({

            _id: { type: String },
            age: { type: Number },
            level_num: { type: Number },
            min_score: { type: Number },
            max_questions: { type: Number }

  });

var Level = mongoose.model('Level', LevelSchema);
</code></pre>

<p>This is child schema</p>

<pre><code>usercurrLevelId: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Level',
        index: true
    }
</code></pre>

<p>This is the level stored in mongoDB</p>

<pre><code>{
"_id" : ObjectId("57cb1ef8b0b05a2ce9b0c24b"),
"age" : 5,
"level_num" : 1,
"min_score" : 12,
"max_questions" : 30
}
{
"_id" : ObjectId("57cb1ef9b0b05a2ce9b0c24c"),
"age" : 5,
"level_num" : 2,
"min_score" : 15,
"max_questions" : 33
}
</code></pre>

<p>This is the child stored in mongoDB</p>

<pre><code>{
"_id" : ObjectId("57cc0410d51045452644251b"),
"usercurrLevelId" : ObjectId("57cb1ef8b0b05a2ce9b0c24b")
}
</code></pre>

<p>And the output i get</p>

<pre><code>{
 "_id": "57cc0410d51045452644251b",
 "usercurrLevelId": null,
 "__v": 0
}
</code></pre>

<p>I using this node js query so that I get level_num and _id from my level schema using this referencing.</p>