1
votes

I need to get the documents as JSON

The view looks like below

function (doc) {
  emit(doc._id); 
}

The design document looks like below

  {
  "_id": "_design/rtls",
  "_rev": "14-cad60060017c38957f6e4388cb37914b",
  "views": {
    "rtls": {
      "map": "function (doc) {\n  emit(doc._id);\n}"
    }
  },
  "language": "javascript"
}

When I get the below data for the below link `http://127.0.0.1:5984/rtls/_design/rtls/_view/rtls?include_docs=true

{"total_rows":4,"offset":0,"rows":[
{"id":"1",
"key":"1",
"value":null,
"doc":{
    "_id":"1",
    "_rev":"39-6ff9597a13572e831728c2e2631eb425",
    "al":[{
         "id16":"0x3f1a",
         "pos":{"x":17.49,"y":10.97,"z":0.01}}],,
"tl":[],
"Timestamp":272179.0,
"Oid":"apoorva-VirtualBox",
"resultTime":"2021-01-03T23:02:29.059Z"}
 }
........
]}

but I need it in the below format

[
{"id":"1",
"key":"1",
"value":null,
"_id":"1",
"_rev":"39",
"al":[{
      "id16":"0x3f1a",
      "pos":{"x":17.49,"y":10.97,"z":0.01}}],
"tl":[],
"Timestamp":272179.0,
"Oid":"apoorva-VirtualBox",
"resultTime":"2021-01-03T23:02:29.059Z"
}
]

I am very much new to CouchDB. Any input is appreciated. Thanks in advance.

1
The view creates an index that exactly matches the main index exposed by the _all_docs endpoint. Why? In addition, why must the output be in the format you need? Have you no control over client processing? - RamblinRose
Because I have named everything the same I guess. I have to read the JSON in Vue.js. if I could get the view to give the data in the JSON format it would be the whole process would be easy. - user11675534

1 Answers

0
votes

CouchDB offers list functions to transform view results. As you can see in the doc these functions are deprecated in CouchDB 3.x and will be removed in 4.x. You can solve a need in the short-term but it is better to not rely on it in the long-term

try to define a list function in your design doc like this one ("lists" element):

   {
  "_id": "_design/rtls",
   "views": {
    "rtls": {
      "map": "function (doc) {\n  emit(doc._id, 1);\n}"
    }
  },
  "lists": {
    "doc-list": "function (head,req) { start({'headers': {'Content-Type': 'application/json'}});  var row; var docs=[]; while (row = getRow()) {docs.push(row.doc)}; send(toJSON(docs));  }"
  },
  "language": "javascript"
}

and query the db with this operation

/db/_design/rtls/_list/doc-list/rtls?include_docs=true

As this is being deprecated it is better to put this logic on your application.