Question

I am looking for a good example how to store votes in a document. For example if we have a document which is post and users can vote for it. If I store the vote in a field in the document, for example:

votes : 12345

What will happen if the author is editing the post and during this time someone votes? The author is not going to be able to save, because somebody voted and document will be with new revision.

The other option is to store votes separately, each vote to be document, or to create a document with votes for every post?

If I decide to store every vote in a different document, how difficult it's going to be to aggregate this data? Or I have to calculate it each time when I show the document?

What are your solutions?

regards

Was it helpful?

Solution

This will result in a conflict. There's a chapter in the CouchDB Guide about handling conflicts. http://guide.couchdb.org/draft/conflicts.html

If you use a middleware (such as PHP) it can recognize and handle the conflict. (see wiki for example code: http://wiki.apache.org/couchdb/Replication_and_conflicts)

If you want to offer a pure CouchApp it should be possible to use update handlers to manage some common conflict cases automatically. http://wiki.apache.org/couchdb/Document_Update_Handlers

If it works I would prefer to store the votes in the document. But I did not try any of these approaches for myself yet. So I would be happy If you share your solution.

OTHER TIPS

I found this article to be very helpful on the subject of how to avoid conflicts when many users will be updating a document, such as voting or adding comments to a blog post.

http://www.cmlenz.net/archives/2007/10/couchdb-joins

The third and best(?) solution was store each comment as a separate document with a link to the blog post. Using complex keys made it very easy to query for all comments belonging to a post as well as querying for all comments made by a user, even sorted in chronological order.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top