我对couchDB非常新,甚至在阅读"how to store hierarchical data"后,它仍然没有点击。从CouchDB检索分层/嵌套数据
而不是使用维基中描述的完整路径模式我希望跟踪孩子作为一个UUID数组和父母作为单个UUID。我倾向于这种模式,所以我可以通过他们在儿童阵列中的位置来维护儿童的顺序。
以下是沙发上的一些示例文档,水桶可以包含水桶和物品,物品只能包含其他物品。 (UUIDs为了清晰起见而缩写):
{_id: 3944
name: "top level bucket with two items"
type: "bucket",
parent: null
children: [8989, 4839]
}
{_id: 8989
name: "second level item with no sub items"
type: "item"
parent: 3944
}
{
_id: 4839
name: "second level bucket with one item"
type: "bucket",
parent: 3944
children: [5694]
}
{
_id: 5694
name: "third level item (has one sub item)"
type: "item",
parent: 4839,
children: [5390]
}
{
_id: 5390
name: "fourth level item"
type: "item"
parent: 5694
}
是否有可能通过嵌入文档ID在地图功能中查找文档?
function(doc) {
if(doc.type == "bucket" || doc.type == "item")
emit(doc, null); // still working on my key value output structure
if(doc.children) {
for(var i in doc.children) {
// can i look up a document here using ids from the children array?
doc.children[i]; // psuedo code
emit(); // the retrieved document would be emitted here
}
}
}
}
在理想世界中,最终的JSON输出看起来像这样。
{"_id":3944,
"name":"top level bucket with two items",
"type":"bucket",
"parent":"",
"children":[
{"_id":8989, "name":"second level item with no sub items", "type":"item", "parent":3944},
{"_id": 4839, "name":"second level bucket with one item", "type":"bucket", "parent":3944, "children":[
{"_id":5694", "name":"third level item (has one sub item)", "type":"item", "parent": 4839, "children":[
{"_id":5390, "name":"fourth level item", "type":"item", "parent":5694}
]}
]}
]
}
感谢您帮助Marcello。当我运行map函数时,输出并不像我希望的那样嵌套,而是完全平坦。有任何想法吗? – berg 2011-05-26 08:01:56
我的答案是[here](http://stackoverflow.com/questions/6084741/how-to-merge-view-collation-into-useful-output-in-couchdb/6094540#6094540)。但我不推荐它。嵌套列表的优点是什么?扁平列表的排列顺序是让每个“项目”或“桶”紧随其子代的请求顺序。遍历此列表非常简单且高效。为什么你需要一个嵌套列表?可能是我可以给你一个更好的解决方案。 – 2011-05-26 15:12:31
我希望直接在我的客户端JavaScript代码中使用结果,这些代码期望数据返回嵌套。但是在阅读了与之相关的问题之后,看起来这与CouchDB的问题背道而驰,所以我打算做这个客户端!再次感谢我将此标记为答案! – berg 2011-05-26 17:39:02