How to restrict deep population in mongoose-autopopulate

北城以北 提交于 2019-12-11 17:18:04

问题


I am using mongoose-autopopulate npm package. In one query I want to deep populate and in another query I want to populate till one level. Here's my schema which is deep populating the data.

 ChildSchema = new Schema({
    plugin_id: { type: String }, 
    title: { type: String }, //card title
    text: { type: String },
    is_valid: { type: Boolean,require:true, default:  false},
    children: [{ type: Schema.Types.ObjectId, ref: 'Child', autopopulate: true }]
});

ChildSchema.plugin(autopopulate);
module.exports = mongoose.model('Child', ChildSchema);

This is Chat History schema:

ChatHistorySchema = new Schema({
    type:{ type:Number }, 
    bot_response:{ type: Schema.Types.ObjectId, ref: 'Child' } 
}); 

This is my query which in which I am trying to disable the auto-populate feature since I don't want deep population instead i want to populate Child schema only to 1 level.

    //query 
  ChatHistory.find({ "bot_user": req.params.botuserId }, {}, {autopopulate: false})
   .populate({
      path: 'bot_response',
      model: 'Child'
   })
   .exec((err,result) => {
    if (err) {
      res.status(500).send({ error: 'Something failed!' })
      deferred.reject(err)
    } else {
      res.json(result)
      deferred.resolve(result);
    }
  })

This query is still returning deeply populated data.

I am not sure how can i get control over maxDepth from query. Any help is highly appreciated.

来源:https://stackoverflow.com/questions/50716368/how-to-restrict-deep-population-in-mongoose-autopopulate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!