2

I have a really simple emit statement in a view.

emit([doc.salesDate, doc.companyId], doc.grossSales);

Is there any way I can make the JSON object it returns show up like this

{
    "grossSales" : "100"
}

instead of

{
    0: "100"
}

EDIT: I'm using the rest API if it makes a difference

mrkwse
  • 420
  • 7
  • 16
Andrew
  • 577
  • 7
  • 20

1 Answers1

2

Ideally you want the view to be as lightweight as possible but you can do this by simply emitting a JSON object

emit([doc.salesDate, doc.companyId], {"grossSales": doc.grossSales});

This assumes the document looks like this:

{
   "salesDate": "2015-06-13T00:27:55.511Z",
   "companyId": "Couchbase",
   "grossSales": 100
}

The output from the REST API:

{"total_rows":1,"rows":[
{"id":"test","key":["2015-06-13T00:27:55.511Z","Couchbase"],"value":{"grossSales":100}}
]
}

Please note that the REST API for views should only be used for testing and debug. In a production environment a SDK should be used.

Paddy
  • 1,195
  • 9
  • 16
  • That's exactly what I was looking for thanks! The couch base documentation isnt the greatest and I couldn't find anything about this. – Andrew Jun 13 '15 at 02:39