Schema 中的递归元素:Mongoose 建模 [英] Recursive elements in Schema : Mongoose modelling
本文介绍了Schema 中的递归元素:Mongoose 建模的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
知道如何在 Mongoose Schema 中对 Tree 文档建模吗?
Any idea how to model a Tree document in Mongoose Schema?
var TreeSchema = new Schema({
"Non-leafNode": {
"children": [
{
"type": "NodeElement"
}
]
},
"NodeElement": {
// one of them is required. not both.
"elem": {
"type": "LeafNode"
},
"elem2": {
"type": "Non-leafNode"
}
},
"LeafNode": {}
});
如何建模?整个树是一个文档(理想情况下).
How could one model this? The entire Tree is one document (ideally).
推荐答案
来自 https://groups.google.com/forum/#!topic/mongoose-orm/0yUVXNyprx8:
按照设计,它可能会起作用.没有这方面的测试.为此,我在其中使用 Schema#add
来生成递归引用:
By design, it might work. There's no tests for this. I have
Schema#add
in there for this purpose, to produce recursive references:
var Tasks = new Schema();
Tasks.add({
title : String
, subtasks : [Tasks]
});
所以你需要一步一步构造递归.
So you need to construct the recursion step by step.
这篇关于Schema 中的递归元素:Mongoose 建模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文