文章詳情頁
javascript - mongoose獲取樹形結構
瀏覽:235日期: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. javascript - 微信網頁開發從菜單進入頁面后,按返回鍵沒有關閉瀏覽器而是刷新當前頁面,求解決?3. node.js - nodejs開發中常用的連接mysql的庫4. 在html文件的目錄下輸入代碼按回車后顯示這個,哪位大佬幫幫我 呀5. mysql - jdbc的問題6. mysql replace 死鎖7. 視頻文件不能播放,怎么辦?8. mysql - 分庫分表、分區、讀寫分離 這些都是用在什么場景下 ,會帶來哪些效率或者其他方面的好處9. windows7 ping不通虛擬機VMware上的linux(ubuntu)的ip10. mysql - 如何減少使用或者不用LEFT JOIN查詢?
排行榜
