1

有 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" : "..."}
     }

]
4

3 回答 3

4

似乎是 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" : "..."}
    }

否则,如果您只希望视图键、值是上述结构,而不关心返回的文档的结构:

地图

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
}

这并不像你想要的那样,但这是我最好的猜测。

于 2013-04-17T23:01:23.607 回答
2

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

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

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

数据建模注意事项适用于 Mongodb,但有关如何在 NoSQL 环境中构建数据的概念与适用于 CouchDB 的概念相同

于 2013-04-22T16:22:26.403 回答
0

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

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

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

于 2020-07-08T04:36:08.373 回答