同一数据库中的 Cloudant 不同文档结构

Cloudant different document structure in the same db

我是 cloudant 的新手,我在同一个数据库中有两个不同的文档结构,如下所示。

我想知道如何列出具有特定结构的所有文档?另一个问题是如何使用 Java 列出文档 2 类型的所有文档以及与对文档 1 类型的特定文档的引用相关的文档?

例如:

文档 1

{
"_id" : "dec_1",
"name" : "value",
"type" : "Value"
}

文件 2 : 其中包含对文件 1 的引用

{
"_id" : "ship_1",
"decRef" : "dec_1",
"size" : "1.0"
}

在 Cloudant/CouchDB 中实现此目的的规范方法是使用指定文档类型的鉴别器字段,然后使用视图。例如:

文档 1:

{
    "_id": "dec_1",
    "name": "value",
    "type": "Value",
    "doctype": "dec"
}

文档 2:

{
    "_id" : "ship_1",
    "decRef" : "dec_1",
    "size" : "1.0",
    "doctype": "ship"
}

然后创建一个地图看起来像这样的视图:

function (doc) {
   if (doc && doc.doctype) {
      emit(doc.doctype, null);
   }
}

现在您可以通过点击视图列出给定类型的所有文档

curl "https://U:P@U.cloudant.com/DB/_design/DDOC/_view/VIEW?key=\"dec\""
{"total_rows":1,"offset":0,"rows":[ 
    {"id":"dec_1","key":"dec","value":null}
]}