Return-Path: Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: (qmail 40468 invoked from network); 27 Sep 2010 00:17:27 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 27 Sep 2010 00:17:27 -0000 Received: (qmail 30984 invoked by uid 500); 27 Sep 2010 00:17:26 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 30927 invoked by uid 500); 27 Sep 2010 00:17:25 -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 30918 invoked by uid 99); 27 Sep 2010 00:17:25 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 27 Sep 2010 00:17:25 +0000 X-ASF-Spam-Status: No, hits=2.5 required=10.0 tests=FREEMAIL_FROM,FREEMAIL_REPLY,NORMAL_HTTP_TO_IP,RCVD_IN_DNSWL_NONE,SPF_PASS,T_TO_NO_BRKTS_FREEMAIL,WEIRD_PORT 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, 27 Sep 2010 00:17:21 +0000 Received: by iwn8 with SMTP id 8so5736606iwn.11 for ; Sun, 26 Sep 2010 17:17:00 -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=3qIqQCAEHzqLHRoXSixyWf4FhvUMYJEjlooGUYcGLtI=; b=c+07CJx/FRqPFe6SlBTYaRtra5JbRaArnWi5VEM3U7iPDpVjk3wXx/6J0Z7X5sa4R+ 6KdSOKYJAQ9ljg4/56MMsxaqrbExbTlgfa3HagvZ2yorlwUfFfTfyqyYvuZ4Ju3hEXiy IDfE1KPm45q3NHOJ+vWAHPqGbyYWG/YvgpQ4Q= 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=qpsbS5ZW6yWD3PP1ThzkFLvY1UxwDf8Q4bgYhB8w+Fpyl8HlfMFKaBQjSW8uqQkRs5 zs3Qr7MqRzCeu+6pFv8WpLf3lIMF1ijmsj7tJcqLx1FO0JNskc7l0/yLqPaMb9s4vgsc 8iHoEz9dg/HjSoAKtWTlMisdC7E6aL3JLkwEE= Received: by 10.231.33.203 with SMTP id i11mr7360433ibd.8.1285546620053; Sun, 26 Sep 2010 17:17:00 -0700 (PDT) MIME-Version: 1.0 Received: by 10.231.30.194 with HTTP; Sun, 26 Sep 2010 17:16:19 -0700 (PDT) In-Reply-To: <69CA7E1C-E158-4FE2-94FB-436D1D4508E2@gmail.com> References: <69CA7E1C-E158-4FE2-94FB-436D1D4508E2@gmail.com> From: Paul Davis Date: Sun, 26 Sep 2010 20:16:19 -0400 Message-ID: Subject: Re: Subselect Possible? To: user@couchdb.apache.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Hrm, I haven't thought this through to the end, but something that might get you started: Change your emit to emit(key, 1); Change your reduce to: function(keys, vals) { return sum(vals); } Then query like such: ?key=3Dquery_key&limit=3D1 ?group_level=3Dlength(query_key)&key=3Dquery_key It seems like there should be a fancy way to get the reduce call to tell you if the original node had < 0 work but I'm distracted by other code right now. HTH, Paul Davis On Sun, Sep 26, 2010 at 7:46 PM, Doug Reeder wrote: > I've searched the wiki and googled for "subselect" and "subquery", and no= t found anything relevelant. > > I'm evaluating whether CouchDB can handle an app which currently stores a= tree structure in a relational database using the "materialized path" repr= esenation. =A0For example, the item with path "QG" is a child of the item w= ith path "Q" and a parent of the item with path "QGC". > > The trickiest query is, in English, "find all items with work less than z= ero and whether they have any children with work less than zero". =A0The SQ= L for this is > SELECT item.*, EXISTS (SELECT work FROM item AS d WHERE d.path > item.pat= h AND d.path <=3D item.path || '\ufffd' AND d.work < 0) AS undonePrereq, FR= OM item WHERE work < 0 > > Can this be done in CouchDB, using a limited, small number of queries? > > My closest sally is the map function > function(doc) { > =A0if (doc.work < 0) { > =A0 =A0var key =3D []; > =A0 =A0var i; > =A0 =A0for (i=3D0; i =A0 =A0 =A0key.push(doc._id.charCodeAt(i)); > =A0 =A0emit(key, doc.work); > =A0} > } > > and the reduce function > function(keys, values, rereduce) { > =A0var shortestKeyLength =3D Infinity; > =A0for (var i=3D0; i =A0 =A0if (keys[i][0].length < shortestKeyLength) > =A0 =A0 =A0 =A0shortestKeyLength =3D keys[i][0].length; > =A0} > > =A0return [keys.length, shortestKeyLength, keys[0][0].slice(0,shortestKey= Length), values.length]; > } > > A query such as > curl -X GET 'http://127.0.0.1:5984/tracker1/_design/trackerA/_view/undone= ArrayNum?startkey=3D\[45,65,65\]&endkey=3D\[45,65,9999\]&group_level=3D2' > will return the data necessary for items at the second level, but the des= ired items could be at any level. > > > > Doug Reeder > reeder.29@gmail.com > http://reeder29.livejournal.com/ > https://twitter.com/reeder29 > > https://twitter.com/hominidsoftware > http://outlinetracker.com > > > > > > > > >