Order of keys in query


While using keys to query i observed based on the order of the keys provided in the request, the output was very different. While the reduce output were in the order of the keys in the request, I also observed for a particular order a key was repeating in the output. I suspect this shouldn’t be the behaviour, somehting I’m missing here?


Hi Krishna,

Is it possible to provide the code for your map reduce view?





Here is my map reduce code with a few keys changed but maintaining the pattern that I suspect is causing the issue

    "map": "function (doc, meta) {if (doc.doc_type == \"user_doc\" && doc.status){if (doc.role == \"Administrator\"){emit(doc.role, {\"id\": doc.id, \"display_name\": doc.display_name, \"email\": doc.email});}else if (doc.role == \"ContentAuthor\"){emit(doc.role, {\"id\": doc.id, \"display_name\": doc.display_name, \"url\": doc.url, \"email\": doc.email});}else if (doc.role == \"ContentPublisher\"){emit(doc.role, {\"id\": doc.id, \"company_name\": doc.company_name, \"display_name\": doc.display_name, \"url\": doc.url, \"email\": doc.email});}}}",
    "reduce": "_count"

Let me know if this is something that could be causing the issue. Even if it is, I believe it shouldn’t be something like order of keys that should be causing a change in the outputindent preformatted text by 4 spaces


Without knowing more about your data set, It’s difficult to know what the problem is. Base don the map reduce:

function (doc, meta) {
if (doc.doc_type == “user_doc” && doc.status) {
if (doc.role == “Administrator”) {
emit(doc.role, {“id”: doc.id, “display_name”: doc.display_name, “email”: doc.email});
} else if (doc.role == “ContentAuthor”) {
emit(doc.role, {“id”: doc.id, “display_name”: doc.display_name, “url”: doc.url, “email”: doc.email});
} else if (doc.role == “ContentPublisher”) {
emit(doc.role, {
“id”: doc.id,
“company_name”: doc.company_name,
“display_name”: doc.display_name,
“url”: doc.url,
“email”: doc.email
}, reduce:"_count"

I suspect there’s something causing duplicate counts within the documents, or there’s a consistency window issue where a document is being added/deleted/changed before the view has updated.

As an alternative, you can try achieving similar functionality using _sum to aggregate by doc.role:

map:function (doc, meta) {
if (doc.doc_type == “user_doc” && doc.status) {



Todd Greenstein