Does CouchDB support multiple range queries?

95 views
Skip to first unread message

Dev

unread,
Jun 25, 2009, 7:44:24 AM6/25/09
to CouchRest


How are multiple range queries implemented in CouchDB? For a single
range condition, startkey and endkey combination works fine, but the
same thing is not working with a multiple range condition.Here i am
using Couch-
rest with rails (Couch Db Ver 0.9.0)

My View function is like this:

"function(doc){
if ((doc['couchrest-type'] == 'Item')
&& doc['loan_name']&& doc['loan_period']&&
doc['loan_amount'])
{ emit([doc['template_id'],
doc['loan_name'],doc['loan_period'],
doc['loan_amount']],null);}}"

I need to get the whole docs with loan_period > 5 and loan_amount >
30000. My startkey and endkey parameters are like this:

params = {:startkey =>["7446567e45dc5155353736cb3d6041c0",nil,
5,30000],
:endkey=>["7446567e45dc5155353736cb3d6041c0",{},{},{}],:include_docs
=> true}

Here, I am not getting the desired result. I think my startkey and
endkey params are wrong. Can anyone help me?

candlerb

unread,
Jun 25, 2009, 11:04:23 AM6/25/09
to CouchRest
This question would be better asked on the couchdb-user mailing list
(see couchdb.apache.org)

>      {  emit([doc['template_id'],
>           doc['loan_name'],doc['loan_period'],
>            doc['loan_amount']],null);}}"
>
> I need to get the whole docs with loan_period > 5 and loan_amount >
> 30000.

What you are asking for is view intersections, which couchdb does not
currently support. You need to do this processing client-side; either
get all the docs with loan_period > 5 and filter them locally for
loan_amount > 30000, or vice versa. In either case you'll need a
different view to the one you already have; either
emit(doc.loan_period,null);
or
emit(doc.loan_amount,null);

so that you can ask for startkey=5 or startkey=30000

> My startkey and endkey parameters are like this:
>
> params = {:startkey =>["7446567e45dc5155353736cb3d6041c0",nil,
> 5,30000],
>   :endkey=>["7446567e45dc5155353736cb3d6041c0",{},{},{}],:include_docs
> => true}

What you have asked for is all documents between key [a,b,c,d] and
[w,x,y,z], and that's exactly what you'll get. All keys are sorted
into a single linear list, so you'll get everything in between these
values. e.g. if the emitted keys look like this:

[a,a,a,a]
[a,b,b,b]
[a,b,c,d] \
[a,f,g,h] |
[b,a,a,a] | these keys fall within the range given
[c,a,z,a] | so will be returned by the query
[w,a,a,z] |
[w,x,y,z] /
[w,z,a,a]

For more info see "view collation" on the CouchDB wiki:
wiki.apache.org/couchdb

Dev

unread,
Jun 25, 2009, 11:19:53 AM6/25/09
to CouchRest
Thanx for your reply.now i got the exact answer
Reply all
Reply to author
Forward
0 new messages