couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sean Copenhaver <>
Subject Re: couchDB and node.js double bind
Date Sun, 22 May 2011 18:12:05 GMT
I hope I understand what you are after, but here's what I got. You have a parent document that
has a list of ids for it's children or some sort of related documents? You want to be able
to query for the parent in the relationship and get a list of children ids with a short description?

Sounds like you need to reverse the relationship (or make it bi-directional). Have the children
keep an id to their parent and use something like the wiki lays out:

With that you should be able to query for the parent and get everything you wanted. For the
children your map function would emit a value of the description you are after. For the parent
the value emitted might be it's doc. Then if you need to turn that into one big document or
some other format you can use a list function:

I hope that helps.

Sean Copenhaver
Sent with Sparrow
On Sunday, May 22, 2011 at 1:55 PM, Ryan Ramage wrote: 
> You create a view, emitting all the docs you will need and the key
> will be the primary id that you will query on, and then create a list
> function over top? Then you would only have one thing to query.
> On Sun, May 22, 2011 at 11:43 AM, MK <> wrote:
> > I'm pseudo cross posting this to the nodejs list, but the replies will
> > not cross-post so hopefully no one is too troubled by that.
> > 
> > I'm new to both and I've hit a bit of a conundrum.
> > 
> > I have a node request handler that presents a view of a couchDB
> > document. The document contains an array of other document id's. In
> > the view, I want to list those documents, and include a piece of
> > information from them (call it the "short description"). This means,
> > in addition to querying couch for the primary doc, I have to query it
> > for the short description of all the docs listed in the primary doc's
> > array of related pages.
> > 
> > Sounds simple. I have the array, now all I have to do is
> > 
> > var shortDescs = new Array();
> > for (i in array) {
> > //query couch for data
> > shortDescs.push(data)
> > }
> > 
> > Except the only way to query couch from node is via asynchronous http
> > requests. Ie, when this loop completes, it is very unlikely that the
> > shortDescs array will be properly populated. So this is a dead end.
> > 
> > That's the way nodejs works, and it has that limitation. The way couch
> > works also results in some limitations. You cannot make a single
> > request for an arbitrary set of documents. You can set up a "view"
> > function that will return data from all the documents that match a
> > specific criteria *but that criteria must be hardcoded*. In other
> > words, you cannot create a view function that takes arbitrary
> > parameters, such as an array of ids.
> > 
> > In short, AFAICT, it is simply impossible to do this using couchDB and
> > nodejs together, which seems a shame.
> > 
> > I can see three choices to work around this:
> > 
> > - Make the primary document's array an array of objects which contains
> > the short desc from the other docs. Obviously, this will result in a
> > tremendous amount of reduplication and wasted space if every doc has an
> > array of related docs that must contain information from those other
> > docs because I cannot query couch for an array of arbitrary docs by id.
> > 
> > - Do a whole bunch of ajax calls when the page loads to complete the
> > information for each doc in the array, because I cannot use node to
> > assemble information from an arbitrary array of http requests to
> > couchDB. Obviously, this will result in increased server load.
> > 
> > - call a couch view to return all short descriptions +
> > ids indiscriminately, and then choose the ones I want. Probably the
> > best choice, but IMO still a very poor compromise.
> > 
> > Is there any other solution anyone sees here?
> > 
> > --
> > "Enthusiasm is not the enemy of the intellect." (said of Irving Howe)
> > "The angel of history[...]is turned toward the past." (Walter Benjamin)

  • Unnamed multipart/alternative (inline, 8-Bit, 0 bytes)
View raw message