Database / CouchDB Interview Questions
What are CouchDB design documents and what do they contain?
Design documents are special CouchDB documents whose IDs begin with _design/. They live in the same database as regular documents but hold server-side JavaScript code that CouchDB's query server executes. Updating a design document invalidates and rebuilds all its associated indexes.
A design document can contain the following sections:
views— MapReduce index definitions. Each view has amapfunction and an optionalreducefunction.indexes— Mango (json/text) index definitions for the_findendpoint.validate_doc_update— a JavaScript function that runs before any document is saved; throw an error to reject the write.filters— JavaScript functions used to filter which documents are replicated or streamed via the_changesfeed.updates— update handler functions that let you perform server-side document transformations via a POST request.listsandshows— legacy functions (deprecated in 3.x) for server-side rendering of view results and individual documents as HTML/XML/text.
{
"_id": "_design/orders",
"views": {
"by_status": {
"map": "function(doc){ if(doc.type==='order') emit(doc.status, doc.total); }",
"reduce": "_sum"
}
},
"validate_doc_update": "function(newDoc, oldDoc, userCtx){ if(!newDoc.type) throw({forbidden:'type required'}); }",
"filters": {
"pending_only": "function(doc, req){ return doc.type==='order' && doc.status==='pending'; }"
}
}
Design documents are versioned just like regular documents and replicate alongside data documents. Changing a design document in a replicated database will propagate the new index definitions to all replica nodes.
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...
