From user-return-11136-apmail-couchdb-user-archive=couchdb.apache.org@couchdb.apache.org Fri Jun 25 16:53:26 2010 Return-Path: Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: (qmail 11444 invoked from network); 25 Jun 2010 16:53:26 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 25 Jun 2010 16:53:26 -0000 Received: (qmail 16374 invoked by uid 500); 25 Jun 2010 16:53:25 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 16325 invoked by uid 500); 25 Jun 2010 16:53:24 -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 16317 invoked by uid 99); 25 Jun 2010 16:53:24 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 25 Jun 2010 16:53:24 +0000 X-ASF-Spam-Status: No, hits=0.9 required=10.0 tests=AWL,FREEMAIL_FROM,SPF_PASS,T_TO_NO_BRKTS_FREEMAIL X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of misterpib@gmail.com designates 209.85.160.180 as permitted sender) Received: from [209.85.160.180] (HELO mail-gy0-f180.google.com) (209.85.160.180) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 25 Jun 2010 16:53:19 +0000 Received: by gyg13 with SMTP id 13so8227033gyg.11 for ; Fri, 25 Jun 2010 09:52:58 -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; bh=BVZIqM8o2S4V/7W5Uk5q90tt7jtXVb6FpAp0m7IZqzw=; b=N7rp6TCAiiAbn2fvX5u4y0hU3NjM7PAwyC2O9dLjvXdcLjuM8GQ0hx45ppBW8aJAaA yZ2k2ONmWJzymKe+VAP66haSG0jI7Q4bo5o04AXYP8KnxMAUUktiyAP1QflH7ACZ/rdW PgUKAvIZHRk/Dpsc5HW/sdA0+3WZCNttxLzvQ= 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; b=h1gszgVxw5gR0pEjGNWb+3HF+GSMrJ0IaHUt3aEI5nqxpLSWn95mXlZkbxWaAn6i5H ofE+HAAq/U4nvzEa6l16lpfeabJkoItdvPXrl2BQrNrmN1w1rJKYryM8+gBBqNA4kvXo tK0N7bhbXiLrI8Hrtz8CIplyNEJYL66YU1ZtM= Received: by 10.224.43.197 with SMTP id x5mr788756qae.127.1277484776492; Fri, 25 Jun 2010 09:52:56 -0700 (PDT) MIME-Version: 1.0 Received: by 10.229.229.129 with HTTP; Fri, 25 Jun 2010 09:52:36 -0700 (PDT) In-Reply-To: References: From: Paul Bonser Date: Fri, 25 Jun 2010 11:52:36 -0500 Message-ID: Subject: Re: Need to "subquery" in a view? To: user@couchdb.apache.org Content-Type: text/plain; charset=ISO-8859-1 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: function(doc) { if (doc.type == 'user') emit([0, doc._id], null); else if (doc.type == 'asset') emit([1, doc.ownerid], null); } Then if you want to get the user and the assets they own, you would query the view with startkey=[0, {userid}] and endkey=[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}]. On Fri, Jun 25, 2010 at 11:40 AM, Sean Coates wrote: > ("subquery" for lack of a better term) > (Apologies if this is a duplicate message. I subscribed in the wrong order.) > > Hello. > > I asked this in #CouchDB, but Jan suggested I also email it here. > > I have a problem where I have data in couch, and I want to perform what seems like one too many leaps of logic from within a view. Here's some sample data: > > [ > {"_id":1,"_rev":1,"type":"user","following":[2,3]}, > {"_id":2,"_rev":1,"type":"user","following":[1]}, > {"_id":3,"_rev":1,"type":"user","following":[1]}, > {"_id":4,"_rev":1,"type":"asset","ownerid":1}, > {"_id":5,"_rev":1,"type":"asset","ownerid":1}, > {"_id":6,"_rev":1,"type":"asset","ownerid":2}, > {"_id":7,"_rev":1,"type":"asset","ownerid":3}, > {"_id":8,"_rev":1,"type":"asset","ownerid":3} > ] > I want to query for a given id where type=user, and get all type=asset records owned by that user, or by users that the queried user is following. Here are some examples that hopefully help: > > _id=1; want documents with IDs: [4,5,6,7,8] > _id=2; want documents with IDs: [4,5,6] > _id=3; want documents with IDs: [4,5,7,8] > I hope that's clear enough. if not, please let me know, and I'll try to expand my explanation. > > The foundation of the problem seems to be that from within a map, I can't fetch secondary type=user documents in order to perform what amounts to a join (in SQL-speak). > > Any help is appreciated. I'm currently solving this problem on the app side, but that's far from ideal (causes a mess with things like limits, for example). > > S > > -- Paul Bonser http://probablyprogramming.com