Database / CouchDB Interview Questions
What is a list function in CouchDB and when would you use it?
A list function is a server-side JavaScript function stored in a design document under the lists key. It acts as a streaming transformer for view query results — instead of returning raw JSON rows, it lets you produce any output format (HTML, XML, CSV, plain text) directly from CouchDB without an intermediary application server.
When called, the list function receives the view result rows one at a time via the getRow() function and can write arbitrary output using send(), building up the response incrementally. This streaming model means large result sets do not need to be buffered in memory.
// In _design/reports, "lists" section:
{
"as_csv": "function(head, req) { start({'headers':{'Content-Type':'text/csv'}}); send('id,status,total\n'); var row; while(row = getRow()) { send(row.id+','+row.value.status+','+row.value.total+'\n'); } }"
}
# Call the list function against a view
GET /db/_design/reports/_list/as_csv/by_status?include_docs=false
When to use list functions: They were popular in CouchApps (self-contained web apps hosted entirely inside CouchDB) where HTML was served from list functions. They can also transform view output to feed legacy systems expecting XML or CSV without an application layer.
Deprecation status: List functions are deprecated in CouchDB 3.x along with show functions. The recommended replacement is to query views from your application server and perform the transformation there. The JavaScript query server adds latency and complexity compared to doing the same transformation in your application code.
More Related questions...