You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Brad Anderson <br...@sankatygroup.com> on 2008/02/29 05:40:01 UTC

nested docs / comments view

Hi,

I'm trying to use recursion in a view... maybe.

Actually, I'm trying to use one GET to retrieve a document and its  
(possibly nested) snippets.

{_id: "TestDoc", type: "doc", title: "Test Title"}

{_id: "snip1", type: "snip", parent: "TestDoc", contents: "...",  
order: 1}
{_id: "snip1a", type: "snip", parent: "snip1", contents: "...", order:  
1}
{_id: "snip1b", type: "snip", parent: "snip1", contents: "...", order:  
2}
{_id: "snip2", type: "snip", parent: "TestDoc", contents: "...",  
order: 2}

So, I tried cmlenz's blog post with View Collation (http://www.cmlenz.net/archives/2007/10/couchdb-joins 
), but it only did one level.  I like the approach of the snippets  
being their own docs, because I think (hope) the update volume on  
those will be high.

I'm wondering if there are other goodies like map() available in view  
functions.  Will I need to call this()?  Should I have the child doc /  
snip keep track of its parent's ids as I've laid out above?  It seems  
like this might lead to a lot of calls instead of the single call I  
want to make.

It just doesn't feel right to have the parents keep track of its  
children's ids, for chance of orphaning, maint. nightmares, and more.   
Who knows, maybe it's a better approach.  Even if I take that route,  
when iterating thru the children of a doc, how do I recursively call  
the function?  Can there be subfuns in Javascript (well, CouchDB views)?

Or a totally new approach is warranted?

Cheers,
BA



Re: nested docs / comments view

Posted by Jan Lehnardt <ja...@googlemail.com>.
Hi,
On Feb 29, 2008, at 05:40, Brad Anderson wrote:
>
> It just doesn't feel right to have the parents keep track of its  
> children's ids, for chance of orphaning, maint. nightmares, and  
> more.  Who knows, maybe it's a better approach.  Even if I take that  
> route, when iterating thru the children of a doc, how do I  
> recursively call the function?  Can there be subfuns in Javascript  
> (well, CouchDB views)?


For the threaded discussion demo I did, I saved all the parent  
document ids in the child document. If you create a view indexed on  
the parent list (and a timestamp) you get a nice threaded threaded  
list of documents. Finally with a bit of startkey= and enkey=  
trickery, you can limit it down to "one parent and all its children".

This is obviously not very friendly towards changes to the tree  
structure where you move nodes and/or subtrees, but that is still  
possible.

Cheers
Jan
--