Ok. Where would it be appropriate to build such a tree? Can I do it in a list function? I'm using Couch directly and not interacting with it using other languages, so I need Couch to do all the work. On Wed, May 16, 2012 at 3:14 PM, Dirkjan Ochtman wrote: > On Wed, May 16, 2012 at 2:53 PM, Luca Matteis wrote: >> Isn't there a better way? > > Since the ordering of descendants depends on the scores of ancestors > (if you're talking about getting the whole thread in order), and you > don't want to update descendants when ancestors get modified (scores), > there by definition is no way to cleanly do this. I think the best way > you can do it is to emit the [ancestor_id, score] for each comment, > which I think would allow you to build the tree structure in a single > loop over all documents: > > docs, roots = {}, [] > for key, doc in documents: >    if key[0] == null: >         roots.append(doc.id) >    docs[doc.id] = doc >    if key[0]: >         docs[key[0]].children.append(doc.id) > > (In horrible pseudo-Python-JS.) > > Cheers, > > Dirkjan