incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From James Marca <>
Subject Re: General Q about CouchDB
Date Wed, 17 Dec 2008 18:30:43 GMT
On Wed, Dec 17, 2008 at 01:49:20AM +0100, Jan Lehnardt wrote:
> On 16 Dec 2008, at 20:54, Christopher McComas wrote:
> >Chris,
> >Thanks. One question, concern I might have with that would be just
> >spelling something differently, but that shouldn't be too big of an
> >issue.
> >
> >To my next question, what would be the best way to structure
> >comments for a blog post, where they have their own author,
> >timestamp, and entry?  Again, this is fairly straight-forward with
> >a relational db using a foreign key.
> Same concept ;)
> See for details.

Apologies for forking a topic slightly, but this maps onto a problem I
am having.  And apologies if this has been answered.  I'm new here, I
*did* look, but I haven't a solution I like yet.

The article's suggested solution will allow comments nested one-layer
deep.  Am I missing something, or is it nearly impossible to collect
comments on comments in one go?  My thought would be to replace "post"
with "parent", but then the view map can't build the sort order
properly, no?

For example:

  "_id": "ABCDEF",
  "_rev": "123456",
  "type": "comment",
  "post": "myslug",
  "author": "jack",
  "content": "…"}
}, {
  "_id": "DEFABC",
  "_rev": "123456",
  "type": "comment",
  "post": "myslug",
  "parent": "myslug",
  "author": "jane",
  "content": "…"
}, {
  "_id": "FABC1234",
  "_rev": "123456",
  "type": "comment",
  "post": "myslug",
  "parent": "DEFABC",
  "author": "john",
  "content": "…"

Winging it with untested code, the best guess I can make for nested
sorting is something like:

function(doc) {
  if (doc.type == "post") {
    emit([doc._id, 0], doc);
  } else if (doc.type == "comment") {
    if(doc.parent == null ||{
	 // could have a date here for the second sort key?
         emit([, doc._id, 1], doc);
         // this fails for arbitrarily deep nesting.  

As I understand it, the problem is that without storing the complete
hierarchy of comments, you can't reproduce the correct nested sorting
in one go.  To quote the "how to store hierarchical data" page in the
wiki, "Store the full path to each node as an attribute in that node's

On the other hand, a perfectly valid solution that uses client-side
javascript to build the doc (this is a blog after all) would be to
just use dom functions to append to parents, something like 

jQuery.each(commentArray, function(){
         .append("<div id='"+this._id+"'class='comment'>"

While this makes it possible to nest comments on the page of
most browswers that support jQuery etc., my real question is about the
inner workings of couchdb, whether it is possible to make the sort
with some clever view definition trickery.

Note that I have absolutely zero clue about reduce functions and their
uses.  Maybe you can use reduce to generate arbitrarily deep nesting
of comments with just a "parent" field??


> Cheers
> Jan
> --
> >
> >
> >Thanks,
> >
> >On Tue, Dec 16, 2008 at 2:51 PM, Chris Anderson <>  
> >wrote:
> >
> >>On Tue, Dec 16, 2008 at 11:46 AM, Christopher McComas
> >><> wrote:
> >>>Would it be wrong to try to do the category piece as related in  
> >>>CouchDB?
> >>>What would be the best way to do it, so that you can have a page,
> >>> that'd then display all the  
> >>>entries
> >>for
> >>>that category? What would be proper?
> >>
> >>Having a category field on the blog post itself is a fine way to do  
> >>this.
> >>
> >>Eg:
> >>
> >>{
> >>"title":"Blah",
> >>"author":"Chris",
> >>"category":"music",
> >>"date": ...
> >>}
> >>
> >>Writing a view that sorts posts by category and date would be simple
> >>with this sort of data structure. Of course if you wanted to rename a
> >>category later you'd need to touch all the documents that listed it,
> >>so this solution is more like tagging than categories, but should
> >>fulfill the need.
> >>
> >>
> >>--
> >>Chris Anderson
> >>
> >>

This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

View raw message