亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 1408 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號