Search code examples
mapreducecouchbasecouchbase-view

couchbase view using multiple keys to get result


I have the following document

{

   "Credit_Amount": 99,
   "Acc_no": 138,
   "Job_No": "esmwga",
   "Source_No": "x",
   "Temp": 1017,
   "Document_No": "gaf",
   "Debit_Amount": 67,
   "User_Id": "xirbzsewiw"
}

and my map function is this

function (doc, meta) {
    if(doc.Type == "GLEntry")
    {
      emit([doc.Acc_no,doc.User_Id],[doc.Credit_Amount,doc.Debit_Amount]);
    }
}

and this is my reduce function

function(key,values,rereduce){
  var sum1=0,sum2=0;
  for(var i=0;i<values.length;++i)
  {
    sum1+=values[i][0];
    sum2+=values[i][1];
  }
  return ([sum1,sum2])
    }

when I pass this key

[138,"xirbzsewiw"]
group level 2

I get this output

[ 99, 67 ]

But When I give this as key

[138]
group level 1

I get empty result. But what I have understood is it will group using only acc number when I give group level 1 so it should give same output. Am I doing something wrong?


Solution

  • Abhi is correct, the result set for your specified key is empty so the reduce is also empty. You can check that by querying with reduce=false.

    You are probably confused from another question you asked where you are using startkey and endkey to get a range. With startkey and endkey you do not need to specify exact keys, the first partial match will be treated as the start or end. In your example if you query with startkey=[138]&endkey=[139]&inclusive_end=false you should see the result you expect.