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.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
