Database / CouchDB Interview Questions
What is the Mango query language in CouchDB and how does it differ from MapReduce views?
Mango is CouchDB's declarative, MongoDB-inspired query language introduced in CouchDB 2.0. Instead of writing JavaScript map functions, you POST a JSON selector document to the _find endpoint. CouchDB evaluates the selector against a Mango index (or falls back to a full scan) and returns matching documents.
POST /mydb/_find
{
"selector": {
"type": "order",
"status": "pending",
"total": { "$gt": 100 }
},
"fields": ["_id", "customer_id", "total", "created_at"],
"sort": [{ "created_at": "desc" }],
"limit": 20,
"skip": 0
}
Key differences between Mango and MapReduce views:
| Aspect | Mango (_find) | MapReduce Views |
|---|---|---|
| Syntax | JSON selector — no JavaScript required | JavaScript map/reduce functions |
| Primary use | Ad-hoc filtering and sorting on arbitrary fields | Pre-aggregated sorted indexes; efficient range queries |
| Aggregation | No built-in aggregation; returns documents | Yes — _sum, _count, _stats reduce functions |
| Index type | Mango JSON index or full-text index | Persistent sorted B-tree |
| Fallback without index | Full database scan (slow — avoid in production) | N/A — view always has an index |
| Best for | Flexible ad-hoc queries, REST APIs, search | Reporting, aggregations, sorted lookups by known key |
Mango is generally the right choice for new applications because it requires no JavaScript and works well for the typical document-filtering use cases. Use MapReduce views when you need server-side aggregation (sums, counts) or must query by a complex compound key with range semantics.
More Related questions...