如何在 couchDB 视图中引用其他文档(加入类似功能)

发布于 2024-10-10 19:48:49 字数 1385 浏览 0 评论 0原文

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

A-> B-> C

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

我们用 type 属性在 CouchDB 中表示这 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",
}

我想要做的是生成一个视图,该视图返回与 A 类型类似的文档,但在逻辑映射列表中包含来自 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...
        }
    }
}
}

我正在努力寻找执行此操作的最佳方法。看起来应该很简单!

We have a CouchDB representation of an XML database which we use to power a javascript based front-end for manipulating the XML documents. The basic structure is a simple 3 level hierarchy. i.e.

A -> B -> C

  • A: Parent document (type A)
  • B: any number of child documents of parent type A
  • C: any number of child documents of parent type B

We represent these 3 document types in CouchDB with a type attribute:

e.g.

{
"_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",
}

What I want to do is produce a view which returns documents just like the A type but includes the label attribute from the B document within the logicalMap list e.g.

{
"_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...
        }
    }
}
}

I'm struggling to get my head around the best way to perform this. It looks like it should be fairly simple though!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白衬杉格子梦 2024-10-17 19:48:49

查看 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 进行查询

Have a look at the "Linked Document' Section in 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']});
        }
    }
}

(untested)

Then query with include_docs=true

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文