Return-Path: X-Original-To: apmail-couchdb-user-archive@www.apache.org Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id C6FFA7EB2 for ; Fri, 9 Dec 2011 16:49:57 +0000 (UTC) Received: (qmail 66306 invoked by uid 500); 9 Dec 2011 16:49:56 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 66276 invoked by uid 500); 9 Dec 2011 16:49:56 -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 66268 invoked by uid 99); 9 Dec 2011 16:49:56 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 09 Dec 2011 16:49:56 +0000 X-ASF-Spam-Status: No, hits=-0.7 required=5.0 tests=RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of zachary.zolton@gmail.com designates 209.85.161.180 as permitted sender) Received: from [209.85.161.180] (HELO mail-gx0-f180.google.com) (209.85.161.180) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 09 Dec 2011 16:49:51 +0000 Received: by ggnq1 with SMTP id q1so4606954ggn.11 for ; Fri, 09 Dec 2011 08:49:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=KgJnVWUWwCel/PbsNJZjoE6ZMs69gOCRH6lUeKbXQU8=; b=HQAFzvadPCfd94106jIlwqQRMTNsp6nadXyrftvzE6vKbmQuqULkAwDlG6bVMKAfbj QAe2PJWNRAtbFebHDwYG5jFrXHsNrEHiC6Z0Gt1TYCwNOJ+mO+LwmsjCI6gPPM7wkeU/ 5gAIjzAIDmQt9T/dFUPuPCNTLHo0XAK8DtA1A= Received: by 10.68.189.232 with SMTP id gl8mr7441048pbc.19.1323449370873; Fri, 09 Dec 2011 08:49:30 -0800 (PST) MIME-Version: 1.0 Received: by 10.143.69.19 with HTTP; Fri, 9 Dec 2011 08:48:59 -0800 (PST) In-Reply-To: <20111209114251.GA5674@urvas> References: <20111209114251.GA5674@urvas> From: Zachary Zolton Date: Fri, 9 Dec 2011 10:48:59 -0600 Message-ID: Subject: Re: advice on how to get equal number of items of different types from db To: user@couchdb.apache.org Content-Type: text/plain; charset=ISO-8859-13 Content-Transfer-Encoding: quoted-printable Bryan, The CouchDB way to get back three different types of data would be to make three view queries. If your problem is that it feels inelegant to deal with those asynchronous calls, then you should look into an asynchronous flow control library. Here are a couple options: jQuery Deferreds (in 1.5+) http://www.erichynds.com/jquery/using-deferreds-in-jquery/ Flow-JS https://github.com/willconant/flow-js Cheers, Zach On Fri, Dec 9, 2011 at 5:42 AM, Rogut=EBs Sparnuotos wrote: > bryan rasmussen (2011-12-09 10:01): >> Hi, >> >> I have a db where items in the db are of different types, lets say >> type A, B, and C. >> There are a lot of items in the db, and I am returning 60 at a time in >> my view. I could theoretically end up in a situation where my first >> 1000+ results are of type A when what I want =A0is an even mix of type >> A,B and C. >> >> I could of course do a view for each type but this is problematic in >> that my view is called at the application's load via Ajax, and I don't >> want to send off 3 requests at that time. >> >> So what I want is a view or mapreduce or other Couchdb functionality >> that allows me to return a mix of my types? >> >> Any pointers, suggestions? >> >> Thanks, >> Bryan Rasmussen > > I would be more worried about 3 separate views than about 3 requests > (especially when they are async). > > 1. Create one view like > =A0 =A0 emit([doc.type, doc.sort_criteria], null) > =A0 and query it like > =A0 =A0 GET ?startkey=3D["A"]&endkey=3D["A", {}]&limit=3D10 > =A0 =A0 GET ?startkey=3D["B"]&endkey=3D["B", {}]&limit=3D10 > =A0 =A0 GET ?startkey=3D["C"]&endkey=3D["C", {}]&limit=3D10 > 2. Get everything client side and filter through. > 3. Make your documents help you. For example, if I needed to return a > =A0 matching triplet of A, B, C, I would keep an index or something: > =A0 =A0 {"type": "A", "typeindex": 1} > =A0 =A0 {"type": "B", "typeindex": 1} > =A0 =A0 {"type": "C", "typeindex": 1} > =A0 =A0 {"type": "A", "typeindex": 2} > =A0 and in a view: > =A0 =A0 emit(doc.typeindex + doc.type, null) > > -- > -- =A0Rogut=EBs Sparnuotos