文章详情页
javascript - mongoose获取树形结构
浏览:356日期: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. 修改mysql配置文件的默认字符集重启后依然不生效2. mongodb - windows7下mongod无法正常启动3. angular.js - 关于指令link 中的创建变量问题4. mysql - 请教一个Java做数据库缓存的问题5. php - 类似Apple官网顶部3级导航该如何设计数据库?6. javascript - ueditor引入报错问题7. python sqlite3 长语句插入出错8. 请问一下各位老鸟 我一直在学习独孤九贱 现在是在tp5 今天发现 这个系列视频没有实战9. javascript - 豆瓣的这个自适应是怎么做的?10. 作为新手,未定义索引username,求解,谢谢
排行榜

网公网安备