Map-reduce grouping with compound keys

I’m having trouble figuring out the best way to accomplish a simple query within a date range:

Given an emit like so:

emit([text1, text2, year, mon, day], null)

and using the builtin _count reduce with the following query:

?group_level=2&startkey=[“text1”,null,2014,7,1]&endkey=[“text1”,"/u0fff",2014,7,10]

I see that because the index is concatenated together and sorted, my date ranges are completely ignored.

Is there no way to accomplish such a simple usecase? The alternative I thought of was to invert the emit and put the dates first, but I do not want to group by dates. I merely would like to filter the date range and only group by my 2 parameters. I guess if I can specify that the grouping start at an offset to my keys, then I could accomplish what I need…

Thanks for your assistance!

The query you have here will include years outside of 2014. For example, the key [“text1”,“foobar”,2013,01,01] would be included in this aggregate data.

Your probably going to want to emit the date only, do a range scan on the date range desired with reduce=false and then do the aggregate part in code. Otherwise there’s N1QL, which will do the same thing via a declarative query.

Also, don’t disregard RDBMS. NoSQL is NOT the best approach for every situation.

1 Like

Thanks. I think it would be a great addition to couchbase if we were able to separate out indexes to some degree. I understand the additional indexing tradeoff, but if kept in balance with application demand it is a boon to developers.

i.e. something that will allow the use of more than ONE index on a view to facilitate more powerful query filtering.

emit[[{compound_index1},{compound_index2}], null]
1 Like