2

I am trying to display the results the substring value of one of my properties.

My data has records with a field called serialNumber, and I have a view like this that returns those records that have "XY" in the field:

function (doc, meta) {
if (doc.serialNumber.indexOf("XY") > -1
    && doc.online
   )
   {
      emit(meta.id, doc.online);
   }
}

What I would like to have displayed in the results as well is the third and fourth characters of the serialNumber. I have tried this:

var prefix = SUBSTR(doc.serialNumber,2,2);
emit(meta.id, doc.online, prefix);

And this:

emit(meta.id, doc.online, doc.serialNumber.substr(2,2);

And also this:

emit(meta.id, doc.online, SUBSTR(doc.serialNumber,2,2));

But the results come back empty.

Thanks.

FuzzyAmi
  • 7,543
  • 6
  • 45
  • 79

1 Answers1

3

The emit function takes 2 parameters: key and value. You can't emit 3 - but you can group your 2 values into a list and emit that:

try this:

emit(meta.id, [doc.online, doc.serialNumber.substr(2,2)]);

Also: I usually use the slice(y,x) function. As in:

doc.serialNumber.slice(2,2)

FuzzyAmi
  • 7,543
  • 6
  • 45
  • 79