note that the Couchbase team is working on a new query language that will allow you do query JSON document easily take a look to the developer preview and online demonstration here:
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.