Return-Path: Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: (qmail 88132 invoked from network); 20 Nov 2009 12:16:24 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 20 Nov 2009 12:16:24 -0000 Received: (qmail 52740 invoked by uid 500); 20 Nov 2009 12:16:23 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 52653 invoked by uid 500); 20 Nov 2009 12:16:23 -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 52636 invoked by uid 99); 20 Nov 2009 12:16:23 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 20 Nov 2009 12:16:23 +0000 X-ASF-Spam-Status: No, hits=-2.3 required=5.0 tests=AWL,BAYES_00 X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of matt.goodall@gmail.com designates 209.85.219.210 as permitted sender) Received: from [209.85.219.210] (HELO mail-ew0-f210.google.com) (209.85.219.210) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 20 Nov 2009 12:16:20 +0000 Received: by ewy2 with SMTP id 2so76583ewy.35 for ; Fri, 20 Nov 2009 04:15:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:content-type :content-transfer-encoding; bh=8u8Q7Ebbw3/yx/LsqMn673mFL58VK5ZpB3K9KlIw5iY=; b=MeSi0zpxe1XdpULwx+ZiqxO49fK+aBXplx4smSolhaZvMFyja9EJ8rKTVTQISlXDgy RuO/FyGk5PmXc/+IXTkHUWoDNkPDcqVZTT2lM/lxkSunXvzzPAjSpqhVv+6iSXelHh+e ttd+R93SUIojsVA9bz9JhsZCfwyub9d/pv328= 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:content-transfer-encoding; b=CrtdXqgrs0EUhBLFRyPZGzb/teSdvKMzoXU1i8CpRwVZsb/bRi3gy+Qzg9zPibclOD YijZd1NqEBc4jnJiuRDPCbiLzVRhnykEI6kUe7k8AjvScUQi0eFQwYm/du0fUw8rCFI8 n2PUQQRg+GYM7qoGLz1y0DkmVAaUF6LsHxPSQ= MIME-Version: 1.0 Received: by 10.213.96.202 with SMTP id i10mr94466ebn.99.1258719359360; Fri, 20 Nov 2009 04:15:59 -0800 (PST) In-Reply-To: <20091120124342.GA6038@seblaptop> References: <20091120112631.49640@gmx.com> <20091120124342.GA6038@seblaptop> Date: Fri, 20 Nov 2009 12:15:59 +0000 Message-ID: <214c385b0911200415p18f6fa92yf69026ecfb7bec89@mail.gmail.com> Subject: Re: Newbie :Filtering using complex key and array From: Matt Goodall To: user@couchdb.apache.org Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 2009/11/20 Sebastien PASTOR : > Sorry for the previous blank mail ... here is the content : > > Hi there, > > Pretty new to couchDB. I ve read a lot about couchdb and finally dive > into it with a small project :) > I am trying to do a simple thing and i am not sure at all if i am going > the right way : > > my docs look like this : > > { > =C2=A0"name":"Pizza Torino", > =C2=A0 "delivery_areas":[75019,75018,75012,75013,75010], > =C2=A0 =C2=A0 "type":"italian" > =C2=A0 =C2=A0 } > > =C2=A0 =C2=A0 I managed to get =C2=A0all shops by type and get a reduce f= unction to > =C2=A0 =C2=A0 do the sum ( not much i know but still quite an accomplishm= ent for > =C2=A0 =C2=A0 me :) ) > =C2=A0 =C2=A0 I then tried to get my result filtered by delivery_areas. a= s in > =C2=A0 =C2=A0 getting only shop > =C2=A0 =C2=A0 that do delivery in postal code 75019. I just could not =C2= =A0have > =C2=A0 =C2=A0 anything that > =C2=A0 =C2=A0 worked using startkey and endkey ... is it the way to go or= is > =C2=A0 =C2=A0 storing > =C2=A0 =C2=A0 delivery_areas within an array not right ? > > =C2=A0 =C2=A0 my last map function looks like this : > =C2=A0 =C2=A0 =C2=A0 =C2=A0 "getShops" : { > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0"map" : "fu= nction(doc){ > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 emit([doc.deliver= y_areas,doc.type],doc.name) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 } > =C2=A0 =C2=A0 } > > =C2=A0Thanks for pointing me to the right direction Storing a list of delivery areas is good. The "trick" is to emit multiple rows per document, e.g. function(doc) { for each (area in doc.delivery_areas) { emit([area, doc.type], doc.name); } } You can then query with startkey=3D[75018, null] and endkey=3D[75018, {}] to get all restaurants in the 75018 area. The null and {} might look a bit weird at first but it's all to do with how CouchDB orders rows in a view. See http://wiki.apache.org/couchdb/View_collation#Collation_Specification for details. Hope this helps. - Matt