Search code examples
couchdbvotedocument-database

How to store votes for CouchDB document?


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


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.