null key in from map/reduce result in couchdb
Asked Answered
Z

1

14

For some reason I'm only getting a null key from map/reduce result in couchdb on mac

Result:

{"rows":[
{"key":null,"value":2224}
]}

Im using couchapp v8.1 and couchdb v1.0.2

My map function is:

function(doc) {
    emit(doc.doc_type, 1);
}

My reduce function is:

function(key, values, rereducer) {
  return sum(values);
}

result I was expecting:

{"rows":[
{"key":"type1","value":22}
{"key":"type2","value":32}
{"key":"type3","value":25}
 //snip snip
]}

Any idea where I went wrong?

Zahn answered 22/5, 2011 at 13:3 Comment(0)
Z
41

Just to save newbies the pain I've been through to get to this answer, please look at the group parameter of the _view API which is explained in more detail in the CouchDB guide.

I just add ?group=true to my url, and the reduce result was grouped as expected the default behavior is that reduce will spit out a single value.

Zahn answered 22/5, 2011 at 18:8 Comment(5)
+1 Good advice! I find myself always adding ?reduce=false even if I don't have a reduce function, just to be sure.Twigg
@jhs: FWIW, ?reduce=false used to break when no reduce function was defined, back in 0.11 (I'm glad it was solved).Multiplication
Don't understand it's not in the default query... Also is not mentioned in the doc.couchbase.com...Mcferren
You saved my day. Thanks!Fleshy
Day saver. Thanks!Danielson

© 2022 - 2024 — McMap. All rights reserved.