结构如下var LabelSchema = new mongoose.Schema({ name: String, parent: {type: ObjectId, ref: 'Label', default: null}, children: [{type: ObjectId, ref: 'Label'}]})希望一次性获取完整的树形结构Label.find({parent: null}) .populate('children') .exec(function(err, labels) { if (err) { console.log(err) } // res.send('test') res.send({ msg: true, result: labels }) })使用了populate方法,但是只能获取第一层的childern引用,第二层的childern仍然是objectId;除了自己通过objectId查找对象,还有没有其他更简便的方法获取完整树形结构?
1 回答
慕田峪7331174
TA贡献1828条经验 获得超13个赞
在find的时候先populate
pointSchema.pre('find', function(next) {
this.populate('children')
next()
})
添加回答
举报
0/150
提交
取消
