結構如下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查找對象,還有沒有其他更簡便的方法獲取完整樹形結構?
mongoose獲取樹形結構
拉莫斯之舞
2018-11-17 13:11:29