Database / CouchDB Interview Questions
What is the _all_docs endpoint in CouchDB and how does it differ from a custom view?
The _all_docs endpoint is a built-in view that CouchDB automatically maintains for every database. It returns all non-deleted documents sorted by their _id (ascending by default). Internally it is backed by the same document B-tree that stores the documents themselves, so it is always up to date with zero additional index maintenance cost.
# Retrieve all documents (just metadata by default)
curl "http://admin:pass@localhost:5984/mydb/_all_docs?limit=10"
# Include full document bodies
curl "http://admin:pass@localhost:5984/mydb/_all_docs?include_docs=true&limit=10"
# Range query by _id prefix (using the collation order of strings)
curl "http://admin:pass@localhost:5984/mydb/_all_docs?startkey=%22order:%22&endkey=%22order:%22&include_docs=true"
# Fetch specific documents by ID (bulk read equivalent to GET on each)
curl -X POST http://admin:pass@localhost:5984/mydb/_all_docs?include_docs=true \
-H "Content-Type: application/json" \
-d '{"keys":["order:001","order:002","user:42"]}'
Differences from a custom view:
_all_docsis always current — no lazy build delay on first access.- It is keyed only by
_id. You cannot query by any other field — for that you need a view or Mango index. - A custom view can emit any key (category, date, compound key) and can aggregate values via reduce.
_all_docscannot. _all_docsincludes design documents; you can filter them out by requestingstartkey="a"(design docs start with_, which sorts before alphabetic characters in CouchDB's collation).
More Related questions...