如何在Couchdb视图中调用另一个视图? [英] How can i call another view in a couchdb view?

查看:179
本文介绍了如何在Couchdb视图中调用另一个视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚读完《 couchdb:权威指南》这本书,并开始阅读设计文档.但是有一件事,我不明白.到目前为止,我所看到的所有示例都是线性的.

I have just finished the book "couchdb: a definitive guide" and started to play with design documents. there is however one thing, that I do not understand. All the examples I have seen so far are somewhat linear.

示例:

{
   "_id": "1",
   "_rev": ".....",
   "name": "first",
   "something": "blue",   
   "child": "2"   
}

{
   "_id": "2",
   "_rev": ".....",
   "name": "second",
   "something": "green",   
   "child": "3"   
   "parent" : "1"
   }

{
   "_id": "3",
   "_rev": ".....",
   "name": "second",
   "something": "red",   
   "parent" : "2";
}

我写一个视图没有问题,该视图返回所有颜色:

I have no problem writing a view, which returns all colors:

function(doc) {
        if (doc.something) {
            emit(doc.something,doc._id);    
    }
}

但是,如果我想知道_id = 1(某物":蓝色")的元素的所有后代(!)后代(不是子代,对不起我的错误)怎么办?我的编程经验告诉我,我应该使用递归,但是我不知道该怎么做.如何从视图函数调用另一个视图函数?

But what if I want to know all (!) descendants (not children, sorry my mistake) for the element with the _id = 1 ("something": "blue")? My programming experience tells me, that i should use recursion, but I do not know how. How can I call another view function, from a view function?

通常:当您使用json文档之间的引用设计数据库时,会出现此问题.更具体地讲,元素之间具有传递关系.

修改: 例如:我只知道_id = 1,结果应该类似于[_id = 2,_id = 3],因为2是1的子代,3是2的子代.

For the example: I only know _id=1 and the result should be something like [_id=2, _id=3], because 2 is a child of 1 and 3 is a child of 2.

推荐答案

如果有可能,不要以这种方式定义文档层次结构-您将在方法的每一步中与CouchDB进行斗争.

If at all possible, don't define the document hierarchy this way -- you'll be fighting CouchDB every step of the way.

您不能真正在视图中进行层次结构遍历.视图用于将每个文档独立地转移到另一个文档上(映射),并从中生成一些汇总值(缩减).

You can't really do the hierarchy walk in a view. Views are meant for transferring each document independently on others (map) and generating some aggregate value from them (reduce).

您可以使用列表同时处理多个文档,但这也不是一个好的解决方案.

You could use lists to operate on multiple documents at the same time, but that's not a good solution either.

如果您需要保留此数据结构(链接到父/子),建议您从CouchDB外部组装该结构:获取父文档,获取其子级,获取其子级,等等.

If you need to keep this data structure (links to parent/child), I suggest you get assemble the structure from outside of CouchDB: get the parent document, get its children, get their children, etc.

但是,在CouchDB中存储树的首选方法是让每个节点记住它在树中的路径:

However, the preferred way of storing a tree in CouchDB is to have each node remember it's path in the tree:

{
   "_id": "1",
   "name": "first",
   "something": "blue",
   "path": [1]
}

{
   "_id": "2",
   "name": "second",
   "something": "green",
   "path": [1,2]
   }

{
   "_id": "3",
   "name": "second",
   "something": "red",
   "path": [1,2,3]
}

然后您可以使用此视图获取文档的后代:

You can then use this view to get a document's descendants:

function(doc) { 
    for (var i in doc.path) { 
        emit([doc.path[i], doc.path], doc) 
    } 
}

要获取_id 1的后代,可以运行以下查询:

To get descendants of _id 1 you can run this query:

http://c.com/db/_design/colors/_view/descendants?startkey=[1]&endkey=[1,{}]

但是,存储完整路径也有其自身的缺点.我建议您检查关于树的CouchDB Wiki页面.来源是 Paul Bonser撰写的此博客文章.

Storing a full path has its own drawbacks, too, though. I suggest you check this CouchDB wiki page on trees. The source for that is this blog post by Paul Bonser.

这篇关于如何在Couchdb视图中调用另一个视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆