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?