reduce function always pass null as a key
Hi,
I found a strange behavior of using map/reduce functionality within a view.
I have the following data:
key1: { "countries": [ "US", "GB", "ZA", "NZ", "CA", "AU" ], "type": "t", "id": "5039451" }, key2: { "countries": [ "RU", "US", ], "type": "t", "id": "5039451" } .... .... ....
I created a view with map function:
function (doc, meta) { if (doc.type != "t") { return; } for(var ck in doc.countries) { emit(doc.countries[ck],doc.id); } }
and I add the following reduce one:
function(key, values, rereduce) { var result = {ids:new Array(),count:0}; for(i=0; i < values.length; i++) { if(rereduce) { result.ids.push(values[i].ids); result.count = result.count + values[i].count; } else { result.ids = values; result.count = values.length; } } return result; }
My goal is getting something like the following view structure:
RU: { ids: ["5039451", "5039451"] count: 2 } ....
But I got a strange result:
key: null(undefined), value: { "ids": [ [ "5039451", "5039451", ..... , "5039451", "5039451", "5039451", "5039451","5039451" ] ], "count": 275 }
Well, I tried to remove reduce function, and noticed that map function works well. But when I add reduce function it always pass null for key param.
It take a place even if I use built in functions such as _count. For above example for _count reduce it looks like:
Key: null(undefined), Value: 275
It looks like a bug but may be I did something wrong......
I have the following configuration:
Couchbase: Version: 2.0.0 community edition (build-1723) OS: CentOS release 5.5 (Final) uname -a: Linux test5.orbitsoft 2.6.18-194.el5 #1 SMP Fri Apr 2 14:58:14 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux
I'm getting the same thing, there might be an actual issue with the version I'm using:
Version: 2.0.0 community edition (build-1723)
I've created a view in the beer sample database with the following Map function:
function (doc, meta) {
emit(doc.type, null);
}
If I don't add a reduce function, I get a view with:
"beer"... for each record, and null for value (as it should be considering the emit).
If I change the Reduce function to _count (and nothing else), I get the following result:
{"rows":[
{"key":null,"value":7315}
]
}
Let me know what I'm doing wrong - but I read and re-read the documentation for the last 4 hours trying to figure out why the key goes null, then I found this post.
You'll want to read the section about ?group=true|false and ?group_level=N parameters.
When not specified explicitly, group is set to false, which means it gives you the reduce value for all keys in the index, regardless of the key value - that's why the response has a JSON key with null value.
Try with ?group=true and you'll see keys you emitted in your map function.
You guys are a life saver! Thank you very much! The option you recommended worked exactly as you described!
Hello,
The value and type of key, depends on whether it's a reduce or a rereduce, and depends on how we're querying (full set, key range) - reduces/reduces can happen at query time when not grouping by partial/full key and/or when using together range parameters (startkey, startkey_docid, endkey, endkey_docid)
Works exactly the same as in CouchDB, for which you have the following comprehensive documentation about the reduce function:
http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views#Reduce_Func...
Hope it helps better understanding it, but it takes some time as not everything is very intuitive and it's very connected to the modified b+tree data structure Damien invented (applying MapReduce to b+trees).