javascript - mongoose獲取樹(shù)形結(jié)構(gòu)
問(wèn)題描述
結(jié)構(gòu)如下
var LabelSchema = new mongoose.Schema({ name: String, parent: {type: ObjectId, ref: ’Label’, default: null}, children: [{type: ObjectId, ref: ’Label’}]})
希望一次性獲取完整的樹(shù)形結(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;除了自己通過(guò)objectId查找對(duì)象,還有沒(méi)有其他更簡(jiǎn)便的方法獲取完整樹(shù)形結(jié)構(gòu)?
問(wèn)題解答
回答1:找到解決方法了,在find的時(shí)候先populate
pointSchema.pre(’find’, function(next) { this.populate(’children’) next()})
相關(guān)文章:
1. php - 微信開(kāi)發(fā)驗(yàn)證服務(wù)器有效性2. 求救一下,用新版的phpstudy,數(shù)據(jù)庫(kù)過(guò)段時(shí)間會(huì)消失是什么情況?3. javascript - 我的站點(diǎn)貌似被別人克隆了, google 搜索特定文章,除了域名不一樣,其他的都一樣,如何解決?4. mysql - 請(qǐng)問(wèn)數(shù)據(jù)庫(kù)字段為年月日,傳進(jìn)的參數(shù)為月,怎么查詢那個(gè)月所對(duì)應(yīng)的數(shù)據(jù)5. Python2中code.co_kwonlyargcount的等效寫(xiě)法6. python - 如何判斷字符串為企業(yè)注冊(cè)名稱7. html - 移動(dòng)端radio無(wú)法選中8. javascript - vue+iview upload傳參失敗 跨域問(wèn)題后臺(tái)已經(jīng)解決 仍然報(bào)403,這是怎么回事啊?9. [python2]local variable referenced before assignment問(wèn)題10. python中怎么對(duì)列表以區(qū)間進(jìn)行統(tǒng)計(jì)?
