文章詳情頁
javascript - mongoose獲取樹形結構
瀏覽:258日期: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. 數(shù)組按鍵值封裝!2. 關docker hub上有些鏡像的tag被標記““This image has vulnerabilities””3. docker不顯示端口映射呢?4. Docker for Mac 創(chuàng)建的dnsmasq容器連不上/不工作的問題5. java - 阿里的開發(fā)手冊中為什么禁用map來作為查詢的接受類?6. python3.x - git bash如何運行.bat文件?7. python - flask _sqlalchemy 能否用中文作為索引條件8. dockerfile - 我用docker build的時候出現(xiàn)下邊問題 麻煩幫我看一下9. nignx - docker內nginx 80端口被占用10. android - 百度地圖加載完成監(jiān)聽
排行榜

熱門標簽