【问题标题】:How to link more documents in one view如何在一个视图中链接更多文档
【发布时间】:2013-04-10 20:56:35
【问题描述】:

有 3 种 CouchDB 文档类型:

用户

{ "_id: "user/author1", "type: "user", "name" : "Joe Doe"}

评论

{ "_id: "review/review1", "type" : "review", "content" : "..." }

产品

{ "_id": "product/awesomeproduct", "type" : "product", "reviews": [
   {
       "author": "author1",
       "review_id": "review/review1"
   },
   {
       "author": "author2",
       "review_id": "review/review2"
   }
  ]
}

如何从单一视图的文档中获取所有数据?

我尝试使用链接文档,但每次发出时只能使用一个(使用 include_docs=true):

(咖啡稿)

(doc) ->
  if doc.type is "product"
    for review in doc.reviews
       emit [doc._id, review.author],{_id: "user/" + review.author}
       emit [doc._id, review.author],{_id: review.review_id}

但我想要这样的东西(在单次​​发射中获取用户和审查文档):

 (doc) ->
  if doc.type is "product"
    for review in doc.reviews
       key = [doc._id, review.author]
       value = {"user:" {_id: "user/" + review.author},"review" :{_id: review.review_id}}
       emit key, value

有没有可能的方法来实现它?如何在单一视图中加入这些类型的文档?

我想得到这样的结果:

[
  key : ["product/awesomeproduct", "author1"], 
  value : {user: {_id: "user/author1"},review :{_id: "review/review1"}}
  doc: 
     {user: {"_id: "user/author1", "type: "user", "name" : "Joe Doe"},
      review: {"_id: "review/review1", "type" : "review", "content" : "..."}
     }

]

【问题讨论】:

  • 啊哈,好的。我在下面附上了另一个答案。

标签: couchdb


【解决方案1】:

似乎是 reduce 函数的经典案例(抱歉,我没有咖啡 :)):

地图

function(doc) {
    if(doc.type == "product") {
        for(review_i in doc.reviews) {
            var review = doc.reviews[review_i];
            emit([doc._id, review.author],{user: "user/" + review.author, review: review.review_id});
        }
    }
}

减少

function(keys, values) {
    return(values);
}

结果(?reduce=true[&group=exact]):

key                                      value
["product/awesomeproduct", "author2"]    [{user: "user/author2", review: "review/review1"}]
["product/awesomeproduct", "author1"]    [{user: "user/author1", review: "review/review1"}]

您希望发出的数据究竟是什么样的?

回复上述评论 应该是可能的,除了您希望在视图返回的文档中显示数据的方式。 include_docs 表示按原样返回整个文档。似乎您希望在视图和返回的文档中表示相同的数据,因此可能将文档的结构更改为:

_id : ...
data : 
    {
        user: {"_id: "user/author1", "type: "user", "name" : "Joe Doe"},
        review: {"_id: "review/review1", "type" : "review", "content" : "..."}
    }

否则,如果你只希望view key,values是上面提到的结构,而不关心返回的doc的结构:

地图

function(doc) {
    if(doc.type == "product") {
        for(review_i in doc.reviews) {
            var review = doc.reviews[review_i];
            emit([doc._id, review.author],{
                            user: {_id: "user/" + review.author}, 
                            review: {_id: review.review_id}
                            });
        }
    }
}

这应该导致:

curl -X GET http://localhost:5984/testdb2/_design/test/_view/test?include_docs=true\&reduce=false

{
    "offset": 0,
    "rows": [
        {
            "doc": {
                "_id": "product/awesomeproduct",
                "_rev": "2-20f0c6cc663e03ab93f8646f09d87db2",
                "reviews": [
                    {
                        "author": "author1",
                        "review_id": "review/review1"
                    },
                    {
                        "author": "author2",
                        "review_id": "review/review1"
                    }
                ],
                "type": "product"
            },
            "id": "product/awesomeproduct",
            "key": [
                "product/awesomeproduct",
                "author1"
            ],
            "value": {
                "review": {
                    "_id": "review/review1"
                },
                "user": {
                    "_id": "user/author1"
                }
            }
        },
        {
            "doc": {
                "_id": "product/awesomeproduct",
                "_rev": "2-20f0c6cc663e03ab93f8646f09d87db2",
                "reviews": [
                    {
                        "author": "author1",
                        "review_id": "review/review1"
                    },
                    {
                        "author": "author2",
                        "review_id": "review/review1"
                    }
                ],
                "type": "product"
            },
            "id": "product/awesomeproduct",
            "key": [
                "product/awesomeproduct",
                "author2"
            ],
            "value": {
                "review": {
                    "_id": "review/review1"
                },
                "user": {
                    "_id": "user/author2"
                }
            }
        }
    ],
    "total_rows": 2
}

这并不是你要找的东西,但这是我的最佳猜测。

【讨论】:

  • Reduce 函数可以工作,但遗憾的是它不能与 include_docs=true 一起使用。所以我没有从其他文档中获取这些数据。我用我需要的结果更新了帖子。
【解决方案2】:

简短的回答...你不能。 Couchdb 只允许您一次发出一个文档。

有两种方法可以获取您需要的数据。

  1. 保持结构不变,并在您的客户端代码中手动执行连接。
  2. 非规范化您的数据。此处的非规范化类似于将评论放入您的产品文档中。

Data modeling considerations 适用于 Mongodb,但有关如何在 NoSQL 环境中构建数据的概念与适用于 CouchDB 的概念相同

【讨论】:

    【解决方案3】:

    你可以在下面做这样的事情,

    function (doc) {
      if(doc.type is "product" && doc.reviews.length > 0){
        
        for(x=0; x<doc.reviews.length; x++){
           key = [doc._id, review.author]
           value = {"user:" {_id: "user/" + review.author},"review" :{_id: review.review_id}}
           emit ([key, value])
        }
      }
    }
    

    这将在您的相关视图中发出两个文档。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-23
      • 1970-01-01
      • 1970-01-01
      • 2019-10-14
      • 2018-08-13
      • 2016-06-11
      • 1970-01-01
      相关资源
      最近更新 更多