文章詳情頁
javascript - mongoose獲取樹形結構
瀏覽:214日期:2024-03-29 08:29:46
問題描述
結構如下
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:找到解決方法了,在find的時候先populate
pointSchema.pre(’find’, function(next) { this.populate(’children’) next()})
標簽:
JavaScript
相關文章:
1. 輸入地址報以下截圖錯誤,怎么辦?2. angular.js - angularJs ngRoute怎么在路由傳遞空字符串及用ng-switch取得3. javascript - ie11以下單擊打開不了file,雙擊可以。求解?4. vim里的高亮javascript的javascript.vim 已經放到syntax里了,但是不行。5. html5 - video ios不能播放怎么辦?6. android - xml的drawable作背景,是否會產生錯誤7. python - pip install出現下面圖中的報錯 什么原因?8. node.js - node中MYSQL的異步問題9. javascript - 求助一個關于indexedDB的問題10. 我何時應該在Java中使用JFrame.add(component)和JFrame.getContentPane()。add(component)
排行榜
