couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alexandre Leray <>
Subject Implementing a threaded comment application with map/reduce
Date Tue, 18 May 2010 00:24:53 GMT

I just started learning couchdb (so far so good!); please forgive me if 
I'm not crystal clear in my explanations!

I'm trying to implement a threaded commenting system in couch, much like does for django/sql.

I wrote some dummy python code to explain what it is suppose to achieve 
(visible in full color here:

class Comment:
     This represent a comment.
     With Couchdb it would be a document with 2 fields:
         object_id: the id of the commented object
         content: the content of the comment
     and of course the _id of the document.
     So a comment could be attached to any object given its id
     (be the object a comment itself or not)
     def __init__(self, id, object_id, content): = id
         self.object_id = object_id
         self.content = content

# Let's make a fake list of comment.
# The first arg is the id, the second the id of the target object
# and the third is the content of the comment
comments = [
     Comment(2, 1, "This is a first comment on some object"),
     Comment(3, 1, "A second comment on some object"),
     Comment(4, 2, "A comment on the first comment"),
     Comment(5, 2, "A second comment on the first comment"),
     Comment(6, 3, "A comment on the second comment on some object"),

def recurse (object_id):
     recursively fetches an object comments.
     Returns a json of nested comment objects/documents
     comment_list = [item for item in comments if item.object_id == 
     if len(comment_list) == 0:
         return ""
         for c in comment_list:
             return """
                     "id" : "%s",
                     "object_id" : "%s",
                     "content" : "%s",
                     "children": [
             """ % (, c.object_id, c.content, recurse(

# get the comments for the object/document with the id 1
print recurse(1)

How could I implement such a thing using map/reduce?


Alexandre Leray

View raw message