Return-Path: Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: (qmail 10220 invoked from network); 28 Jun 2010 22:10:15 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 28 Jun 2010 22:10:15 -0000 Received: (qmail 26581 invoked by uid 500); 28 Jun 2010 22:10:13 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 26528 invoked by uid 500); 28 Jun 2010 22:10:13 -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 26515 invoked by uid 99); 28 Jun 2010 22:10:13 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 28 Jun 2010 22:10:13 +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 (nike.apache.org: domain of misterpib@gmail.com designates 209.85.212.52 as permitted sender) Received: from [209.85.212.52] (HELO mail-vw0-f52.google.com) (209.85.212.52) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 28 Jun 2010 22:10:06 +0000 Received: by vws3 with SMTP id 3so1164393vws.11 for ; Mon, 28 Jun 2010 15:08:46 -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=AwkxY0aDaCzvNX/bcXUJujzCvtQdcvEgCZXPFfKDhNo=; b=dqkgwxxPB+tWG3I+TQ0lH3AM6RZaMlx2pwuzinDLhIsc1sfOgH+WD5MYQwcj1xj1F7 +xFCwC20+po5Jq+FY+0x3y7n/ajxVdLM8Wiurv7Pez/o0SjQ6zG5671KpF2573Hz/F6/ RKHbhmUhXMsxTiRvZy1vgHiHSPNmREer0y//Y= 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=fLjmp+icyj2PKha/+j4gTyK2qzeIxnYw68NcTNopwXrF31UvJMwrMczy7oiPZwzeaz 6mmN9BdB0gI2PrJtH5ys4fuJ+9hncueLge+yVgAX5mK1dW+8B1b4HeJBSAS5Uj7xZGK8 mmCwn2yKFszkdRHZ7wl5fK5FfXzw6B3pOBWps= Received: by 10.224.61.69 with SMTP id s5mr3837363qah.189.1277762925325; Mon, 28 Jun 2010 15:08:45 -0700 (PDT) MIME-Version: 1.0 Received: by 10.229.229.129 with HTTP; Mon, 28 Jun 2010 15:08:24 -0700 (PDT) In-Reply-To: <904D26CC-327F-4D71-B174-6F5A5E968802@seancoates.com> References: <904D26CC-327F-4D71-B174-6F5A5E968802@seancoates.com> From: Paul Bonser Date: Mon, 28 Jun 2010 17:08:24 -0500 Message-ID: Subject: Re: Need to "subquery" in a view? To: user@couchdb.apache.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Virus-Checked: Checked by ClamAV on apache.org Whoops, I must have been half asleep for that reply..., I got the key backw= ard. it should be "emit([doc._id, 0])" and startkey=3D[{userid, 0}], and extrapolate from that for the rest. Swap the number and the user id. On Mon, Jun 28, 2010 at 10:53 AM, Sean Coates wrote: >> The usual way that's done is talked about here in the "One to Many" >> section: http://wiki.apache.org/couchdb/EntityRelationship >> >> Basically, you map function would be something like: >> >> =A0 =A0function(doc) { >> =A0 =A0 =A0 =A0if (doc.type =3D=3D 'user') emit([0, doc._id], null); >> =A0 =A0 =A0 =A0else if (doc.type =3D=3D 'asset') emit([1, doc.ownerid], = null); >> =A0 =A0} >> >> Then if you want to get the user and the assets they own, you would >> query the view with startkey=3D[0, {userid}] and endkey=3D[1, {userid}], >> and if you just want the list of assets owned by a certain user, you >> use a startkey and endkey of [1, {userid}]. > > > I'm sorry if I'm being thick, but I don't quite understand how this would= work. > Wouldn't a startkey=3D[0,{userid}]&endkey=3D[1,{userid}] match all users = if {userid}=3D"1" ? > > I've adjusted my test data to make it more clear what's going on (see bel= ow #1). u1 is not following u2 (now). > If I query: start_key=3D[0,"u1"]&end_key=3D[1,"u1"] > I get user records for all users (u1, u2, u3). I don't want u2 to be incl= uded. (see below #2) > > Perhaps I'm not doing a very good job of explaining what I need to do. > Does this make any more sense now? > > S > > (1) > {"total_rows":9,"offset":0,"rows":[ > {"id":"_design/test","key":"_design/test","value":{"rev":"1-fecdec6331d23= f13f7f40baef4ca6564"},"doc":{"_id":"_design/test","_rev":"1-fecdec6331d23f1= 3f7f40baef4ca6564","language":"javascript","views":{"join":{"map":" =A0 fun= ction(doc) {\n =A0 =A0 =A0 if (doc.type =3D=3D 'user') emit([0, doc._id], n= ull);\n =A0 =A0 =A0 else if (doc.type =3D=3D 'asset') emit([1, doc.ownerid]= , null);\n =A0 }"}}}}, > {"id":"a4","key":"a4","value":{"rev":"2-e01f856361aef17a8b241114da1bcecb"= },"doc":{"_id":"a4","_rev":"2-e01f856361aef17a8b241114da1bcecb","type":"ass= et","ownerid":"u1"}}, > {"id":"a5","key":"a5","value":{"rev":"3-14db4d330452eb9f157e4a413227f4c8"= },"doc":{"_id":"a5","_rev":"3-14db4d330452eb9f157e4a413227f4c8","type":"ass= et","ownerid":"u1"}}, > {"id":"a6","key":"a6","value":{"rev":"3-e9f5d5c8ceaf8167c06d6751dcefb2df"= },"doc":{"_id":"a6","_rev":"3-e9f5d5c8ceaf8167c06d6751dcefb2df","type":"ass= et","ownerid":"u2"}}, > {"id":"a7","key":"a7","value":{"rev":"3-1706f210111b6fe15dec9d09eb9af47e"= },"doc":{"_id":"a7","_rev":"3-1706f210111b6fe15dec9d09eb9af47e","type":"ass= et","ownerid":"u3"}}, > {"id":"a8","key":"a8","value":{"rev":"3-1706f210111b6fe15dec9d09eb9af47e"= },"doc":{"_id":"a8","_rev":"3-1706f210111b6fe15dec9d09eb9af47e","type":"ass= et","ownerid":"u3"}}, > {"id":"u1","key":"u1","value":{"rev":"6-c3b4277626cd76b76b18b5acbff2edd8"= },"doc":{"_id":"u1","_rev":"6-c3b4277626cd76b76b18b5acbff2edd8","type":"use= r","following":["u3"]}}, > {"id":"u2","key":"u2","value":{"rev":"2-6b91b31ff9679d2a0a989f42a27ca496"= },"doc":{"_id":"u2","_rev":"2-6b91b31ff9679d2a0a989f42a27ca496","type":"use= r","following":["u1"]}}, > {"id":"u3","key":"u3","value":{"rev":"2-6b91b31ff9679d2a0a989f42a27ca496"= },"doc":{"_id":"u3","_rev":"2-6b91b31ff9679d2a0a989f42a27ca496","type":"use= r","following":["u1"]}} > ]} > > (2) > $ curl 'http://localhost:5984/test/_design/t/_view/join?start_key=3D%5B0%= 2C%22u1%22%5D&end_key=3D%5B1%2C%22u1%22%5B' > {"total_rows":8,"offset":0,"rows":[ > {"id":"u1","key":[0,"u1"],"value":null}, > {"id":"u2","key":[0,"u2"],"value":null}, > {"id":"u3","key":[0,"u3"],"value":null}, > {"id":"a4","key":[1,"u1"],"value":null}, > {"id":"a5","key":[1,"u1"],"value":null}, > {"id":"a6","key":[1,"u2"],"value":null}, > {"id":"a7","key":[1,"u3"],"value":null}, > {"id":"a8","key":[1,"u3"],"value":null} > ]} > > --=20 Paul Bonser http://probablyprogramming.com