问题
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