How can I get a view of favorite user documents by user in Couchdb map/reduce?

Posted by Jeremy Raymond on Stack Overflow See other posts from Stack Overflow or by Jeremy Raymond
Published on 2010-03-22T23:54:43Z Indexed on 2010/03/23 2:21 UTC
Read the original article Hit count: 334

Filed under:
|

My Couchdb database as a main document type that looks something like:

{
   "_id" : "doc1",
   "type" : "main_doc",
   "title" : "the first doc"
   ...
}

There is another type of document that stores user information. I want users to be able to tag documents as favorites. Different users can save the same or different documents as favorites. My idea was to introduce a favorite document to track this something like:

{
   "_id" : "fav1",
   "type" : "favorite",
   "user_id" : "user1",
   "doc_id" : "doc1"
}

It's easy enough to create a view with user_id as the key to get a list of their favorite doc IDs. E.g:

function(doc) {
   if (doc.type == "favorite") {
      emit(doc.user_id, doc.doc_id);
   }
 }

However I want to list of favorites to display the user_id, doc_id and title from the document. So output something like:

{ "key" : "user1", "value" : ["doc1", "the first doc"] }

© Stack Overflow or respective owner

Related posts about couchdb

Related posts about mapreduce