我对couchDB很新,甚至在读完 (latest archive as now deleted) http://wiki.apache.org/couchdb/How_to_store_hierarchical_data (via ‘Store the full path to each node as an attribute in that node’s document’)之后它仍然没有点击
而不是使用维基中描述的完整路径模式,我希望将子项跟踪为UUID数组,父项作为单个UUID.我倾向于这种模式,所以我可以通过他们在儿童阵列中的位置维持孩子的顺序.
以下是沙发中的一些示例文档,存储桶可以包含存储桶和项目,项目只能包含其他项目. (UUID缩写为清晰):
{_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
}
是否可以通过map函数中的嵌入文档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}
]}
]}
]
}
你可以找到一般的讨论
on the CouchDB wiki.
我现在没时间测试它,但是你的地图功能应该类似于:
function(doc) {
if (doc.type === "bucket" || doc.type === "item")
emit([ doc._id, -1 ], 1);
if (doc.children) {
for (var i = 0, child_id; child_id = doc.children[i]; ++i) {
emit([ doc._id, i ], { _id: child_id });
}
}
}
}
您应该使用include_docs = true查询它以获取文档,如CouchDB documentation中所述:如果您的map函数发出一个具有{‘_id’:XXX}的对象值,并且您使用include_docs = true参数查询视图,那么CouchDB将获取id为XXX的文档,而不是处理为发出键/值对的文档.
添加startkey = [“3944”]& endkey [“3944”,{}]以仅获取ID为“3944”及其子项的文档.
编辑:有关详细信息,请查看this question.
