how to give muliple composite keys in startkey and endkey to search in views

Hi, i create a view which emit composite keys

function (doc, meta) {
emit([doc.name,doc.sex,doc.age], doc);
}

i want to get results for multiple composite keys. like [['name1','male','99],['name2','female','99']]

i have group of names along with sex but i dont have exact value of age so i want to give age in ranges and i want to sort the result based on age.

Please help me how can i get the result.

note i dont have exact value for age....

1 Answer

« Back to question.

Hello,

One important thing to remember is that all queries you will do in the index/view will be possible from left to right. So if you want to group by gender and age you MUST start with one of these values depending of your business requirement.
Based on your description you will have something like:

function (doc, meta) {
  emit([doc.gender,doc.age], null);
}

In addition to the fact that I change the key, I also emit a null value. You should "never" emit the full document in the view. By doing that you will just double the size of your database. You just need to emit the key of your index, and emit a value when it makes sense for your application: calculation, projection, reduce...

If you want for example all the females you will query using these parameters:

?startkey=["F"]&endkey=["F",{}]

If you want all males between 35 and 45:

startkey=["M",35]&endkey=["M",45]

So as you can see I do a range query using the various values of your compound key.

If you need to get all people (independently of their gender) with an age between 30 and 40 you cannot use this index/view you must create another view that emits either the age only on starts with the age.

I am inviting you to look at the following links:
- http://blog.couchbase.com/understanding-grouplevel-view-queries-compound...
- http://docs.couchbase.com/couchbase-manual-2.2/#views-and-indexes

Regards
Tug
@tgrall