CouchDB 如何在一个视图中链接多个文档

zu0ti5jz  于 2022-12-09  发布在  CouchDB
关注(0)|答案(3)|浏览(124)

有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"
   }
  ]
}

如何使用单一视图从文档中获取所有数据?
我尝试使用链接文档,但每个emit只能使用一个链接文档(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" : "..."}
     }

]
3qpi33ja

3qpi33ja1#

看起来像是reduce函数的经典案例(抱歉,我没有咖啡:)):

Map

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=精确]):

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

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

Map

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
}

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

kg7wmglp

kg7wmglp2#

简短的回答...你不能。Couchdb一次只允许你发出一个文档。
有两种方法可以获取所需的数据。
1.保持结构不变,并在客户端代码中手动执行连接。
1.反规范化您的数据。这里的反规范化类似于将评论放在您的产品文档中。
Data modeling considerations适用于Mongodb,但有关如何在NoSQL环境中构建数据的概念与适用于CouchDB的概念相同

tmb3ates

tmb3ates3#

您可以在下面这样做,

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

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

相关问题