Return-Path: Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: (qmail 67608 invoked from network); 28 Sep 2010 16:45:03 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 28 Sep 2010 16:45:03 -0000 Received: (qmail 8535 invoked by uid 500); 28 Sep 2010 16:45:02 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 8482 invoked by uid 500); 28 Sep 2010 16:45:01 -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 8474 invoked by uid 99); 28 Sep 2010 16:45:01 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 28 Sep 2010 16:45:01 +0000 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests=FREEMAIL_FROM,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 jchris@gmail.com designates 209.85.161.52 as permitted sender) Received: from [209.85.161.52] (HELO mail-fx0-f52.google.com) (209.85.161.52) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 28 Sep 2010 16:44:57 +0000 Received: by fxm17 with SMTP id 17so5190025fxm.11 for ; Tue, 28 Sep 2010 09:44:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:sender:received :in-reply-to:references:date:x-google-sender-auth:message-id:subject :from:to:content-type:content-transfer-encoding; bh=qwMNoaOskHJhHxCbOnz7skImoalXDeR6Fal1vYrbOT4=; b=ezPdEG2V5ZABA40f56rn/PQ/2Ko4uqHP09nQkzYm5EuqrkcE7vTGzLpwd4oaLz4iwS R1BbwskQ6PgBRNZbmjUamJ2eUgvluw6huOiBYNvXgck86TvMvtiERgCOBf9HmuaA0jUf Dccwf/dBGQCvgJksB+UudVwzJVBco79yCpZvs= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:content-type :content-transfer-encoding; b=aChsHsj2NdZ9koYSU6s0rtfeCYTHaB6x6OHAYpIaySAVm71/p8GR7f3tfXqbiy4VMo 7Czl7M0VImpf5kzJFy4j4V7zbZQ49CYjEE834owkoxyFaZ4daFQuaVK7ZbuppXOhO5yg /+jRNmjv3LE38nhdujpxGN7aNHvX53r/Y8tls= MIME-Version: 1.0 Received: by 10.103.229.19 with SMTP id g19mr411041mur.59.1285692274779; Tue, 28 Sep 2010 09:44:34 -0700 (PDT) Sender: jchris@gmail.com Received: by 10.220.42.4 with HTTP; Tue, 28 Sep 2010 09:44:34 -0700 (PDT) In-Reply-To: <21853817-2E46-465C-B324-F5343987B6A2@gmail.com> References: <69CA7E1C-E158-4FE2-94FB-436D1D4508E2@gmail.com> <21853817-2E46-465C-B324-F5343987B6A2@gmail.com> Date: Tue, 28 Sep 2010 09:44:34 -0700 X-Google-Sender-Auth: x3NOl2iwqrF0jM7czkcbtohWK4M Message-ID: Subject: Re: Subselect Possible? From: Chris Anderson To: user@couchdb.apache.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Tue, Sep 28, 2010 at 9:36 AM, Doug Reeder wrote: > The cmlenz post on CouchDB "Joins" is useful; are there other articles on= CouchDB design patterns? > banking transactions: http://guide.couchdb.org/draft/recipes.html#banking sql-ish: http://guide.couchdb.org/draft/cookbook.html > > On Sep 28, 2010, at 11:18 AM, Paul Davis wrote: > >> On Tue, Sep 28, 2010 at 9:04 AM, Doug Reeder wrote= : >>> Sorry, my earlier response seems to have gone astray. >>> >>> Thank you, but this map and reduce function produce exactly the same re= sults as mine. >>> >>> Is there a general method of structuring data and map-reduce functions = to do the same thing that a subselect does? >>> >> >> No, there's nothing in general. SQL is a query language. Views are >> range matches. The question is if you can rephrase your subselect to >> give you the same data using collation. It may seem a bit limiting at >> first, but there are a few different methods for arranging data. >> >> For instance, things like this: >> http://www.cmlenz.net/archives/2007/10/couchdb-joins >> Or this might be helpful: >> http://blog.couchone.com/post/1167966323/a-gentle-introduction-to-couchd= b-for-relational >> >> If you're still not able to get what you want, you might want to try >> couchdb-lucene. >> >> HTH, >> Paul Davis >> >>> >>> On Sep 26, 2010, at 8:16 PM, Paul Davis wrote: >>> >>>> 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) { >>>> =A0 =A0return 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 wro= te: >>>>> I've searched the wiki and googled for "subselect" and "subquery", an= d not found anything relevelant. >>>>> >>>>> I'm evaluating whether CouchDB can handle an app which currently stor= es a tree structure in a relational database using the "materialized path" = represenation. =A0For example, the item with path "QG" is a child of the it= em with path "Q" and a parent of the item with path "QGC". >>>>> >>>>> The trickiest query is, in English, "find all items with work less th= an zero and whether they have any children with work less than zero". =A0Th= e SQL for this is >>>>> SELECT item.*, EXISTS (SELECT work FROM item AS d WHERE d.path > item= .path AND d.path <=3D item.path || '\ufffd' AND d.work < 0) AS undonePrereq= , FROM 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,shortes= tKeyLength), values.length]; >>>>> } >>>>> >>>>> A query such as >>>>> curl -X GET 'http://127.0.0.1:5984/tracker1/_design/trackerA/_view/un= doneArrayNum?startkey=3D\[45,65,65\]&endkey=3D\[45,65,9999\]&group_level=3D= 2' >>>>> will return the data necessary for items at the second level, but the= desired 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 >>> >>> >>> >>> >>> >>> >>> >>> >>> > > Doug Reeder > reeder.29@gmail.com > http://reeder29.livejournal.com/ > https://twitter.com/reeder29 > > https://twitter.com/hominidsoftware > http://outlinetracker.com > > > > > > > > > --=20 Chris Anderson http://jchrisa.net http://couch.io