You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Ning Tan <ni...@gmail.com> on 2010/05/17 17:57:16 UTC

Returning document referenced by _id in view

I remember a "new" feature for the views to return referenced
document(s) if I emit their keys in a certain way, thus saving me a
round trip to load those documents, but I can't find the documentation
describing that feature anymore.

Can somebody help me?

Thanks!

Re: Implementing a threaded comment application with map/reduce

Posted by Jarrod Roberson <ja...@vertigrated.com>.
what you probably need is a View Collation, you don't need a reduce function
you aren't reducing anything.

Implementing a threaded comment application with map/reduce

Posted by Alexandre Leray <ne...@alexandreleray.com>.
Hi,

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 
http://code.google.com/p/django-threadedcomments/ does for django/sql.

I wrote some dummy python code to explain what it is suppose to achieve 
(visible in full color here:
http://dpaste.com/195870/):

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):
         self.id = 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 == 
object_id]
     if len(comment_list) == 0:
         return ""
     else:
         for c in comment_list:
             return """
                 {
                     "id" : "%s",
                     "object_id" : "%s",
                     "content" : "%s",
                     "children": [
                         %s
                     ],
                  },
             """ % (c.id, c.object_id, c.content, recurse(c.id))

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


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

Thanks,

Alexandre Leray



Re: Returning document referenced by _id in view

Posted by Sebastian Cohnen <se...@googlemail.com>.
the wiki is missing this information, will add that somewhere...

On 17.05.2010, at 18:00, Alexander Uvarov wrote:

> 
> On 17.05.2010, at 21:57, Ning Tan wrote:
> 
>> I remember a "new" feature for the views to return referenced
>> document(s) if I emit their keys in a certain way, thus saving me a
>> round trip to load those documents, but I can't find the documentation
>> describing that feature anymore.
>> 
>> Can somebody help me?
>> 
>> Thanks!
> 
> emit(key, { "_id": some_id })
> 
> http://blog.couch.io/post/446015664/whats-new-in-apache-couchdb-0-11-part-two-views
> 
> Cheers.


Re: Returning document referenced by _id in view

Posted by Alexander Uvarov <al...@gmail.com>.
On 17.05.2010, at 21:57, Ning Tan wrote:

> I remember a "new" feature for the views to return referenced
> document(s) if I emit their keys in a certain way, thus saving me a
> round trip to load those documents, but I can't find the documentation
> describing that feature anymore.
> 
> Can somebody help me?
> 
> Thanks!

emit(key, { "_id": some_id })

http://blog.couch.io/post/446015664/whats-new-in-apache-couchdb-0-11-part-two-views

Cheers.