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