You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by bryan rasmussen <ra...@gmail.com> on 2011/12/09 10:01:08 UTC

advice on how to get equal number of items of different types from db

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  is 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

Re: advice on how to get equal number of items of different types from db

Posted by Daniel Bryan <da...@gmail.com>.
A show function is probably the immediate solution, but I'd also suggest thinking about why you're experiencing this problem in the first place:
is the data transmission with the whole document really that much? Is this a security problem?
are you sure you aren't overestimating the overhead of doing several separate AJAX requests? If you're requesting conceptually discrete things then this seems architecturally correct
if you really are going to have high volumes of GET requests for this data, maybe you should consider restructuring your data? Is it really correct for your application logic for it to be organised in this way?




On Saturday, 10 December 2011 at 3:48 AM, Zachary Zolton wrote:

> 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ës Sparnuotos
> <rogutes@googlemail.com (mailto:rogutes@googlemail.com)> 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  is 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
> >     emit([doc.type, doc.sort_criteria], null)
> >   and query it like
> >     GET ?startkey=["A"]&endkey=["A", {}]&limit=10
> >     GET ?startkey=["B"]&endkey=["B", {}]&limit=10
> >     GET ?startkey=["C"]&endkey=["C", {}]&limit=10
> > 2. Get everything client side and filter through.
> > 3. Make your documents help you. For example, if I needed to return a
> >   matching triplet of A, B, C, I would keep an index or something:
> >     {"type": "A", "typeindex": 1}
> >     {"type": "B", "typeindex": 1}
> >     {"type": "C", "typeindex": 1}
> >     {"type": "A", "typeindex": 2}
> >   and in a view:
> >     emit(doc.typeindex + doc.type, null)
> >  
> > --
> > --  Rogutës Sparnuotos
> >  
>  
>  
>  



Re: advice on how to get equal number of items of different types from db

Posted by Zachary Zolton <za...@gmail.com>.
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ės Sparnuotos
<ro...@googlemail.com> 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  is 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
>     emit([doc.type, doc.sort_criteria], null)
>   and query it like
>     GET ?startkey=["A"]&endkey=["A", {}]&limit=10
>     GET ?startkey=["B"]&endkey=["B", {}]&limit=10
>     GET ?startkey=["C"]&endkey=["C", {}]&limit=10
> 2. Get everything client side and filter through.
> 3. Make your documents help you. For example, if I needed to return a
>   matching triplet of A, B, C, I would keep an index or something:
>     {"type": "A", "typeindex": 1}
>     {"type": "B", "typeindex": 1}
>     {"type": "C", "typeindex": 1}
>     {"type": "A", "typeindex": 2}
>   and in a view:
>     emit(doc.typeindex + doc.type, null)
>
> --
> --  Rogutės Sparnuotos

Re: advice on how to get equal number of items of different types from db

Posted by Rogutės Sparnuotos <ro...@googlemail.com>.
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  is 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
     emit([doc.type, doc.sort_criteria], null)
   and query it like
     GET ?startkey=["A"]&endkey=["A", {}]&limit=10
     GET ?startkey=["B"]&endkey=["B", {}]&limit=10
     GET ?startkey=["C"]&endkey=["C", {}]&limit=10
2. Get everything client side and filter through.
3. Make your documents help you. For example, if I needed to return a
   matching triplet of A, B, C, I would keep an index or something:
     {"type": "A", "typeindex": 1}
     {"type": "B", "typeindex": 1}
     {"type": "C", "typeindex": 1}
     {"type": "A", "typeindex": 2}
   and in a view:
     emit(doc.typeindex + doc.type, null)

-- 
--  Rogutės Sparnuotos