-1

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?

Legendary_Hunter
  • 1,040
  • 2
  • 10
  • 29

2 Answers2

1

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.

Kerr
  • 2,792
  • 21
  • 31
0

Here is what I think is probably happening here:

  • Your map function emits keys like: [138,"xirbzsewiw"]
  • You need to pass key that your map function generates as "keys" while doing reduce operations. In 2nd case, you're passing [138], which isn't a legitimate key so it isn't matching anything in index and hence you aren't seeing any output.

group_level does filtering on output i.e. numbers of indexes upto which you want to get from an array of strings. You might want to look at understanding group level view queries, if you haven't already to have better understanding of group_level

Abhi
  • 33
  • 1
  • 4
  • So there is no way I can ignore some right hand key values in emit function? I think I read somewhere that we can skip some of the right hand values but all left hand values should be provided. – Legendary_Hunter Mar 10 '16 at 07:20