1
votes

New to CouchDB after years of MySQL. Why am I getting metadata _id and _rev in the values map in a view the returns all records? Here's my view:

"views": "{
    "all": "{
        "map": "function (doc) {
            emit(doc.date, doc)
        }"
    }"
}"

When I query /database/_design/id/_view/all I get the following JSON:

{"total_rows":2,"offset":0,"rows":[
{"id":"b77bbd5ab73b809ca830fe4b6900506f","key":"[2013, 09, 21, 00, 00, 00]","value":{"_id":"b77bbd5ab73b809ca830fe4b6900506f","_rev":"11-b134dfe46f86c96b4f7e2cf0abb13b61","location":"Mesuk Market","product":"Traditional Pork Skewer","total_items":550,"created_at":"[2013, 09, 21, 23, 24, 06]","date":"[2013, 09, 21, 00, 00, 00]"}},
{"id":"b77bbd5ab73b809ca830fe4b69005f1e","key":"[2013, 09, 21, 00, 00, 00]","value":{"_id":"b77bbd5ab73b809ca830fe4b69005f1e","_rev":"2-ee4e464cdf5de23563c0cc6b62bc4b07","date":"[2013, 09, 21, 00, 00, 00]","created_at":"[2013, 09, 21, 23, 27, 00]","total_items":100,"product":"All Meat Skewer","location":"Mesuk Market"}}
]}

Why am I getting _id and _rev in the map of values? The docs here (and several other places) don't show those fields in the values.

This isn't really blocking my work on a project, but I don't understand why it's happening. Is it from the way I'm setting up my view or making a query?

2

2 Answers

2
votes

The _id and _rev values are needed to CouchDB. The first one, _id (uuid), has to ensure uniqueness of the document in the database, the second one, _rev (revision), has to ensure there are no conflicts while multiple processes try to edit the same document.

These fields are mandatory.

_id is assigned by CouchDB when it's not in the document we try to create, _rev is always assigned by CouchDB.

1
votes

_id and _rev are special document fields:

  • _id is database-wide unique Document ID
  • _rev is MVCC token