Add name attribute to what gets emitted from a view in couchbase

49 views Asked by At

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

1

There are 1 answers

1
Paddy On BEST ANSWER

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.