incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rogutės Sparnuotos <rogu...@googlemail.com>
Subject Re: advice on how to get equal number of items of different types from db
Date Fri, 09 Dec 2011 11:42:51 GMT
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

Mime
View raw message