与 CouchDB 连接/求和
使用 CouchDB,我目前有一个代表一个想法的文档,您可以评价这个想法。每个想法都是一个文档,每个评级都是一个不同的文档。我这样做是为了避免人们在评价一个想法时出现并发访问问题。
我的文档看起来像这样(我已经简化了它们):
一个想法:
{
"_id": "idea1",
"title": "A great idea"
}
评级:
{
"_id": "rating1",
"rating": 1,
"author": "author1"
}
{
"_id": "rating2",
"rating": 1,
"author": "author2"
}
我目前使用一个reduce函数返回我的想法id和他/她的评级(评级的简单总和):
地图:
function(doc) {
if (doc.type == "rating")
emit(doc.idea_id, doc.rating);
}
Reduce:
function(keys, values, rereduce) {
return sum(values);
}
我的问题是:我如何将“想法”文档与代表该想法评级的缩减结果“结合”?
Using CouchDB, I currently have a document which represents an idea, you can rate this idea. Every idea is one document and every rating is a different document. I am doing this like this to avoid concurrent access problems when people are rating an idea.
My documents look like that (I have simplified them):
An idea:
{
"_id": "idea1",
"title": "A great idea"
}
Ratings:
{
"_id": "rating1",
"rating": 1,
"author": "author1"
}
{
"_id": "rating2",
"rating": 1,
"author": "author2"
}
I currently use a reduce function to return me the idea id and his/her rating (a simple sum of the ratings):
Map:
function(doc) {
if (doc.type == "rating")
emit(doc.idea_id, doc.rating);
}
Reduce:
function(keys, values, rereduce) {
return sum(values);
}
My question is: how could I "join" the "idea" document with the reduce result which represent the rating for the idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
映射:
减少:
另一个选择是 使用视图排序规则 来完成这个任务。
Map:
Reduce:
Another option would be to use view collation to do the trick.
首先,请不要创建您自己的 _id,让 CouchDB 为您创建一个 uuid。我向你保证,这要好得多:)
简短的回答是,除了附加查询之外,你无法通过任何其他方式获得想法文档。尽管查询速度相当快,因为您的投票文档中有 _id。
如果你想返回所有投票的完整文件,以获取评论或其他什么,你绝对可以这样做。只需使用 ?include_docs=true 运行视图查询
First off, please don't create your own _id, let CouchDB create a uuid for you. It's much better, I promise you :)
The short answer is, you can't get the idea document with anything other than an additional query. Although the query is quite fast since you have the _id in your vote document.
If you wanted to return the the full documents for all of the votes, in order to grab comments or something, you could definitely do that. Just run the view query with ?include_docs=true