Return-Path: Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: (qmail 67698 invoked from network); 18 Oct 2010 19:37:17 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 18 Oct 2010 19:37:17 -0000 Received: (qmail 74269 invoked by uid 500); 18 Oct 2010 19:37:16 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 74236 invoked by uid 500); 18 Oct 2010 19:37:16 -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 74228 invoked by uid 99); 18 Oct 2010 19:37:16 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 18 Oct 2010 19:37:16 +0000 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests=FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_PASS,T_TO_NO_BRKTS_FREEMAIL X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of paul.joseph.davis@gmail.com designates 209.85.214.180 as permitted sender) Received: from [209.85.214.180] (HELO mail-iw0-f180.google.com) (209.85.214.180) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 18 Oct 2010 19:37:11 +0000 Received: by iwn41 with SMTP id 41so286957iwn.11 for ; Mon, 18 Oct 2010 12:36:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:mime-version:received:in-reply-to :references:from:date:message-id:subject:to:content-type :content-transfer-encoding; bh=4H/TRAAuUkPQAOsWB+vk9+ONsbCJDchMPucf6wn9PwU=; b=X0meUoB70rSCgok/9YATZcZbabjw5xkM48MuWyfrewXpNd6qWxYuSX1kZbCNlIuLcc /2+QcA3JOdhWa1j9oVYxL0u6B0n8OYHbI5ATxtSLzu8gwwfGfP+NehZ3Ko6DZH/AUDc7 pqteAsh/pLYNIv9ZcXf2CTXWmWGEuiNrMuzy4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; b=llPOZOFkxDb/zVrqnpK+MQP1W9grVY4RkHOrH0KRMGRMMhKHjULRsDudYHVltCJd9L QFBYD2774p+f0ya5fGQIihPTGnRS52CT8F6wME8DbQmvfXlTS66fhffKPwu2kOCqd/gA iG/Cpgz3II+fIHTlG31Fv/k5fATSrgp5Sr8Ks= Received: by 10.231.40.3 with SMTP id i3mr3808419ibe.66.1287430610963; Mon, 18 Oct 2010 12:36:50 -0700 (PDT) MIME-Version: 1.0 Received: by 10.231.36.130 with HTTP; Mon, 18 Oct 2010 12:36:08 -0700 (PDT) In-Reply-To: References: From: Paul Davis Date: Mon, 18 Oct 2010 15:36:08 -0400 Message-ID: Subject: Re: Including values from linked documents To: user@couchdb.apache.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Mon, Oct 18, 2010 at 3:05 PM, Tane Piper wrote: > On Mon, Oct 18, 2010 at 4:41 PM, Tane Piper wrote: >> Hi there, >> >> I have a question about views - I've thrown up the code into this >> gist: https://gist.github.com/4cd88fc6a5aac5467774 >> >> What I have is a ExtJS grid that I load these assets in to, each one >> looks like this document and it loads fine - but of course in the >> columns for the three fields location (docType Office), owner and >> delegate (both docType Role) it displays the ID rather than the name. >> >> Is there a way, without having to go out and fetch these documents >> with further requests, that I can include the name from each document >> in this view? >> >> Thanks in advance >> >> - Tane Piper >> > > Ok, I think I managed to figure this out, so I thought I would share > it here and hopefully someone can sanity check it as well in case > there is a better way to do this: > > My map function now looks like this: > > function(doc) { > =A0 =A0if (doc.docType =3D=3D 'Asset') { > =A0 =A0 =A0 =A0emit([doc._id, 'asset'], {_id: doc._id}); > =A0 =A0 =A0 =A0if (doc.location) { > =A0 =A0 =A0 =A0 =A0 =A0emit([doc.location, 'location'], {_id: doc.locatio= n}); > =A0 =A0 =A0 =A0} > =A0 =A0 =A0 =A0if (doc.owner) { > =A0 =A0 =A0 =A0 =A0 =A0emit([doc.owner, 'owner'], {_id: doc.owner}); > =A0 =A0 =A0 =A0} > =A0 =A0 =A0 =A0if (doc.delegate) { > =A0 =A0 =A0 =A0 =A0 =A0emit([doc.delegate, 'delegate'], {_id: doc.delegat= e}); > =A0 =A0 =A0 =A0} > =A0 =A0 } > } > > That produces an output of: > > {"total_rows":8,"offset":0,"rows":[ > {"id":"89dd7ff6553c8cd2d44489c4f304bf33","key":["89dd7ff6553c8cd2d44489c4= f3033d05","location"],"value":{"_id":"89dd7ff6553c8cd2d44489c4f3033d05"}}, > {"id":"89dd7ff6553c8cd2d44489c4f304c93a","key":["89dd7ff6553c8cd2d44489c4= f3033d05","location"],"value":{"_id":"89dd7ff6553c8cd2d44489c4f3033d05"}}, > {"id":"89dd7ff6553c8cd2d44489c4f304bf33","key":["89dd7ff6553c8cd2d44489c4= f3039337","owner"],"value":{"_id":"89dd7ff6553c8cd2d44489c4f3039337"}}, > {"id":"89dd7ff6553c8cd2d44489c4f304bf33","key":["89dd7ff6553c8cd2d44489c4= f303f04f","delegate"],"value":{"_id":"89dd7ff6553c8cd2d44489c4f303f04f"}}, > {"id":"89dd7ff6553c8cd2d44489c4f304c93a","key":["89dd7ff6553c8cd2d44489c4= f303f04f","owner"],"value":{"_id":"89dd7ff6553c8cd2d44489c4f303f04f"}}, > {"id":"89dd7ff6553c8cd2d44489c4f304bf33","key":["89dd7ff6553c8cd2d44489c4= f304bf33","asset"],"value":{"_id":"89dd7ff6553c8cd2d44489c4f304bf33"}}, > {"id":"89dd7ff6553c8cd2d44489c4f304c93a","key":["89dd7ff6553c8cd2d44489c4= f304c93a","asset"],"value":{"_id":"89dd7ff6553c8cd2d44489c4f304c93a"}}, > {"id":"89dd7ff6553c8cd2d44489c4f304c93a","key":["89dd7ff6553c8cd2d44489c4= f304cadd","delegate"],"value":{"_id":"89dd7ff6553c8cd2d44489c4f304cadd"}} > ]} > > Then in my code, I need to loop over it twice to produce the correct > output for my JSON: > > var assets =3D []; > data.rows.forEach(function(row) { > =A0 =A0if (row.key[1] =3D=3D 'asset') { > =A0 =A0 =A0 =A0var asset =3D row.doc; > =A0 =A0 =A0 =A0data.rows.forEach(function(row) { > =A0 =A0 =A0 =A0 =A0 =A0if (row.id =3D=3D asset._id && row.key[1] =3D=3D '= location') > asset.location_display =3D row.doc.name; > =A0 =A0 =A0 =A0 =A0 =A0if (row.id =3D=3D asset._id && row.key[1] =3D=3D '= owner') > asset.owner_display =3D row.doc.name; > =A0 =A0 =A0 =A0 =A0 =A0if (row.id =3D=3D asset._id && row.key[1] =3D=3D '= delegate') > asset.delegate_display =3D row.doc.name; > > =A0 =A0 =A0 =A0}); > =A0 =A0 =A0 =A0assets.push(asset); > =A0 =A0} > }); > You might try instead something like such: function(doc) { if(doc.docType !=3D 'Asset') return; emit([doc._id, 'asset'], {_id: doc._id}); if(doc.location) emit([doc._id, 'location'], {_id: doc.location}); if(doc.owner) emit([doc._id, 'owner'], {_id: doc.owner}); if(doc.delegate) emit([doc._id, 'delegate'], {_id: doc.delegate}); } This way the values for each document are sorted to the same location in the view output. Which also allows you to build your thing for specific assert id's using a query string like: ?start_key=3D[somedoc._id]&end_key=3D[somedoc._id, {}]&include_docs=3DTrue HTH, Paul Davis