【问题标题】:Cloudant different document structure in the same dbCloudant在同一个数据库中不同的文档结构
【发布时间】:2018-10-22 17:54:21
【问题描述】:

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

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

例如:

文档1

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

文档 2:包含对文档 1 的引用

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

【问题讨论】:

    标签: java cloudant


    【解决方案1】:

    在 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}
    ]}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-07
      • 1970-01-01
      • 2015-11-13
      • 2012-07-21
      • 1970-01-01
      • 1970-01-01
      • 2020-08-26
      • 1970-01-01
      相关资源
      最近更新 更多