为了账号安全,请及时绑定邮箱和手机立即绑定

mongoose获取树形结构

mongoose获取树形结构

拉莫斯之舞 2018-11-17 13:11:29
结构如下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()

})


查看完整回答
反对 回复 2018-12-06
  • 1 回答
  • 0 关注
  • 1420 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号