javascript - mongoose獲取樹形結(jié)構(gòu)
問題描述
結(jié)構(gòu)如下
var LabelSchema = new mongoose.Schema({ name: String, parent: {type: ObjectId, ref: ’Label’, default: null}, children: [{type: ObjectId, ref: ’Label’}]})
希望一次性獲取完整的樹形結(jié)構(gòu)
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查找對象,還有沒有其他更簡便的方法獲取完整樹形結(jié)構(gòu)?
問題解答
回答1:找到解決方法了,在find的時(shí)候先populate
pointSchema.pre(’find’, function(next) { this.populate(’children’) next()})
相關(guān)文章:
1. php多任務(wù)倒計(jì)時(shí)求助2. 數(shù)組排序,并把排序后的值存入到新數(shù)組中3. 默認(rèn)輸出類型為json,如何輸出html4. 怎么能做出標(biāo)簽切換頁的效果,(文字內(nèi)容隨動(dòng))5. python的正則怎么同時(shí)匹配兩個(gè)不同結(jié)果?6. PHP訂單派單系統(tǒng)7. python中def定義的函數(shù)加括號和不加括號的區(qū)別?8. javascript - charles map remote映射問題9. mysql - sql 左連接結(jié)果union右連接結(jié)果,導(dǎo)致重復(fù)性計(jì)算怎么解決?10. javascript - 有適合開發(fā)手機(jī)端Html5網(wǎng)頁小游戲的前端框架嗎?
