CouchDB Group Level and Range

Can someone explain to me why the following does not work:

Assuming the following document structure:

{ "_id": "520fb089a6cb538b1843cdf3cca39a15", "_rev": "2-f96c27d19bf6cb10268d6d1c34799931", "type": "nosql", "location": "AZ", "date": "2012/03/01 00:00:00", "amount": 1500 } 

And the Map function, defined as follows:

 function(doc) { var saleDate = new Date(doc.date); emit([doc.location,saleDate.getFullYear(),saleDate.getMonth()+1],doc.amount); } 

And using the built-in _sum function for the gearbox.

When you do this (with group = true), you get these results:

 {"rows":[ {"key":["AZ",2012,2],"value":224}, {"key":["AZ",2012,3],"value":1500}, {"key":["WA",2011,12],"value":1965}, {"key":["WA",2012,1],"value":358} ]} 

Now, if you change the request to something like this:

 http://127.0.0.1:5984/default/_design/nosql/_view/nosql_test?group_level=2 

You get the following results:

 {"rows":[ {"key":["AZ",2012],"value":1724}, {"key":["WA",2011],"value":1965}, {"key":["WA",2012],"value":358} ]} 

So, keeping in mind, if I wanted to find out all the sales in 2011 for "WA", I could not do something like this:

 http://127.0.0.1:5984/default/_design/nosql/_view/nosql_test?group_level=2&key=["WA",2011] 

This example was taken from useful videos on NoSQL feeds.

http://nosqltapes.com/video/understanding-mapreduce-with-mike-miller

+6
source share
1 answer

You always need to specify a range of keys, because filtering is done by map results, not reduce .

For example, the following parameters should work (if correctly encoded by URL):

 ?group_level=2&startkey=["WA",2011]&endkey=["WA",2011,{}] 

You can read the sorting view to understand how this works.

+8
source

Source: https://habr.com/ru/post/907970/


All Articles