Search code examples
couchdbpouchdb

What is the difference between id and key fields in /{db}/_all_docs response?


/{db}/_all_docs response seems to have identical id and key value. What is the difference between the two? The key field is not documented anywhere.


Solution

  • At first glance id and key may seem redundant, however it makes sense when considering _all_docs. From the documentation:

    Executes the built-in _all_docs view*, returning all of the documents in the database. With the exception of the URL parameters (described below), this endpoint works identically to any other view. Refer to the view endpoint documentation for a complete description of the available query parameters and the format of the returned data.

    * emphasis mine

    So _all_docs is a built-in view. Consider the view documentation. It is helpful to think of views as being composed of three fields

    • id
    • key
    • value

    User designed views generally take shape as id = document._id with key and value produced by the emit() function (via the map function).

    For example, the following map function

    function (doc) {
      emit(doc.someField, doc.someValue);
    }
    

    generates the view id = doc._id, key = doc.someField, value = doc.someValue.