join - 如何在 couchDB View 中引用其他文档(加入类似功能)

标签 join view couchdb

我们有一个 XML 数据库的 CouchDB 表示,我们用它来驱动基于 javascript 的前端来操作 XML 文档。基本结构是一个简单的 3 级层次结构。 IE。

A -> B -> C

A:父文件(A 类)
B:任意数量的父类型 A 的子文档
C:任意数量的父类型 B 的子文档

我们在 CouchDB 中用 type 表示这 3 种文档类型。属性:

例如

{
"_id":"llgc-id:433",
"_rev":"1-3760f3e01d7752a7508b047e0d094301",
"type":"A",
"label":"Top Level A document",
"logicalMap":{
    "issues":{
        "1":{
            "URL":"http://hdl.handle.net/10107/434-0",
            "FILE":"llgc-id:434"
        },
        "2":{
            "URL":"http://hdl.handle.net/10107/467-0",
            "FILE":"llgc-id:467" 
        etc...
        }
    }
}
}


{
"_id":"llgc-id:433",
"_rev":"1-3760f3e01d7752a7508b047e0d094301",
"type":"B",
"label":"a B document",
}

我想要做的是生成一个 View ,它像 A 类型一样返回文档,但在 logicalMap 列表中包含来自 B 文档的标签属性,例如
{
"_id":"llgc-id:433",
"_rev":"1-3760f3e01d7752a7508b047e0d094301",
"type":"A",
"label":"Top Level A document",
"logicalMap":{
    "issues":{
        "1":{
            "URL":"http://hdl.handle.net/10107/434-0",
            "FILE":"llgc-id:434",
            "LABEL":"a B document"
        },
        "2":{
            "URL":"http://hdl.handle.net/10107/467-0",
            "FILE":"llgc-id:467",
            "LABEL":"another B document" 
        etc...
        }
    }
}
}

我正在努力寻找执行此操作的最佳方式。不过看起来它应该相当简单!

最佳答案

查看 http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views#Linked_documents 中的“链接文档”部分

function(doc) {
    //....
    if (doc.logicalMap.issues) {
        for (var i in doc.logicalMap.issues) {
            emit([doc._id,doc.logicalMap.issues[i]['FILE']], 
                                 {_id: doc.logicalMap.issues[i]['FILE']});
        }
    }
}

(未经测试)

然后用 include_docs=true 查询

关于join - 如何在 couchDB View 中引用其他文档(加入类似功能),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4625187/

相关文章:

SQL 查询 - 组合 DISTINCT 和 TOP?

postgresql - Postgres View,alter table 更改表名后,View 还查询吗?

ios - 在 UITabBarController 中的 ViewController 中呈现 View

couchdb - 是否有任何文档数据库的设计指南?

database - 使用 cURL 创建 couchdb 独立附件

MySQL根据记录的总和查询一个表或在另一张表中没有记录

mysql - 查询多张表,但只得到一张表的结果,不重复

couchdb - couchdb适合构建wiki引擎吗?

A、B、不同 C 的 SQL 连接

android - 如何在 Android 中使用动画将 View 移动到另一个 View ?