From user-return-16299-apmail-couchdb-user-archive=couchdb.apache.org@couchdb.apache.org Sun May 22 20:50:05 2011 Return-Path: X-Original-To: apmail-couchdb-user-archive@www.apache.org Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id A51DD64DF for ; Sun, 22 May 2011 20:50:05 +0000 (UTC) Received: (qmail 2172 invoked by uid 500); 22 May 2011 20:50:04 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 2139 invoked by uid 500); 22 May 2011 20:50:04 -0000 Mailing-List: contact user-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user@couchdb.apache.org Delivered-To: mailing list user@couchdb.apache.org Received: (qmail 2131 invoked by uid 99); 22 May 2011 20:50:04 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 22 May 2011 20:50:04 +0000 X-ASF-Spam-Status: No, hits=1.5 required=5.0 tests=FREEMAIL_FROM,HTML_MESSAGE,RCVD_IN_DNSWL_LOW,RFC_ABUSE_POST,SPF_PASS,T_TO_NO_BRKTS_FREEMAIL X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of afters.mail@gmail.com designates 209.85.210.180 as permitted sender) Received: from [209.85.210.180] (HELO mail-iy0-f180.google.com) (209.85.210.180) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 22 May 2011 20:49:59 +0000 Received: by iyf40 with SMTP id 40so6276250iyf.11 for ; Sun, 22 May 2011 13:49:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type; bh=Elm3mNNoPEu1rcqVdfTMcvzdZGSr8cG/xFJkGzmGJeA=; b=oKVQJ9iCzEmuechEspBTBdr3JCqwu81Ety8knn+++AE54hppdB8q5LX5m6bTJVoHe2 gjjIca0yfjMqUUVYZ3YZSbQRGN7S7Q34PHq/wWJMg5fG3ivSAJ4SE4KxQz/QiPbFTN/w CwsSy0+TZXk74czyHQRfQQeeK2UEmxdQH8cLw= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=qwWdr2kEPxVaSmrKr/3ZOZx8u3ZaYt1mnWs58lw1r3eYl/Ba5LRTsFubHklmmIqjZ3 gNDH8HiITVbL3b+GNNut/L8nGOsZfLfvhd+piTUQq62yHx23Lkhb8SRvFwGILc4bp7Fw UmqHmfxbryPvk0UdOSfdL+pNDh9K/6epqZIoc= MIME-Version: 1.0 Received: by 10.231.193.233 with SMTP id dv41mr1482398ibb.186.1306097378146; Sun, 22 May 2011 13:49:38 -0700 (PDT) Received: by 10.231.35.75 with HTTP; Sun, 22 May 2011 13:49:38 -0700 (PDT) In-Reply-To: <20110522134328.345ba41b.mk@cognitivedissonance.ca> References: <20110522134328.345ba41b.mk@cognitivedissonance.ca> Date: Sun, 22 May 2011 23:49:38 +0300 Message-ID: Subject: Re: couchDB and node.js double bind From: afters To: user@couchdb.apache.org Content-Type: multipart/alternative; boundary=0016e68ee14a4439b804a3e37ebf --0016e68ee14a4439b804a3e37ebf Content-Type: text/plain; charset=ISO-8859-1 MK, Luckily for you, some of your assumptions are wrong. You should have no problem doing an "async-for-loop" with Node. Read this: http://metaduck.com/post/2675027550/asynchronous-iteration-patterns-in-node-js Also, you might not need a for-loop, as Couch lets you fetch several documents at the same time: http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API#Fetch_Multiple_Documents_With_a_Single_Request Cheers, a. On 22 May 2011 20:43, 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) > > --0016e68ee14a4439b804a3e37ebf--