文章详情页
javascript - mongoose获取树形结构
浏览:271日期: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. 关docker hub上有些镜像的tag被标记““This image has vulnerabilities””2. Android TextView 或 ListView 加载过渡效果3. docker内创建jenkins访问另一个容器下的服务器问题4. docker网络端口映射,没有方便点的操作方法么?5. dockerfile - 为什么docker容器启动不了?6. docker 17.03 怎么配置 registry mirror ?7. 在mac下出现了两个docker环境8. docker-compose 为何找不到配置文件?9. 致命错误: Class ’appfacadeTest’ not found10. html5 - 为什么浏览器可以显示HTML文档中未被定义的标签?
排行榜
