CouchDB Mango and views
The CouchDB wire answers two query languages: Mango selectors (_find, _explain) and JavaScript map/reduce views. Both run inside the wire over documents held in memory; neither produces a LogicalPlan. The route table, persistence, _changes, replication, errors and tests are documented in CouchDB HTTP API implementation reference; this page describes how a query is evaluated.
Translation path
(source: src/protocol/couchdb/api.rs, find; src/protocol/couchdb/mango.rs, validate, matches, collate, project; src/protocol/couchdb/views.rs, refresh, builtin_reduce, js_reduce)
Mango selectors
mango::validate checks the selector before anything runs, so an unknown operator or a wrongly typed argument is a 400 even on an empty database (source: src/protocol/couchdb/mango.rs, validate, FIELD_OPS).
| Kind | Operators |
|---|---|
| Comparison |
$lt, $lte, $eq, $ne, $gte, $gt
|
| Element |
$exists, $type
|
| Membership |
$in, $nin
|
| Array |
$size, $all, $elemMatch, $allMatch
|
| Object | $keyMapMatch |
| Evaluation |
$mod, $regex, $beginsWith
|
| Logic |
$and, $or, $nor, $not
|
(source: src/protocol/couchdb/mango.rs, FIELD_OPS, matches)
A field name is a dotted path; get_path and split_path resolve it (source: src/protocol/couchdb/mango.rs, get_path, split_path). $regex uses the Rust regex crate, whose syntax differs from CouchDB's PCRE in places (source: src/protocol/couchdb/mango.rs, matches).
Comparisons and sorting use view collation: null, then false, then true, then numbers, strings, arrays and objects. Strings compare case-insensitively first, with lowercase before uppercase on a tie; this approximates ICU collation and is not ICU (source: src/protocol/couchdb/mango.rs, collate, collate_str).
_find request
| Field | Handling |
|---|---|
selector |
Required |
limit |
Default 25 |
skip |
Default 0 |
fields |
Projection with mango::project
|
sort |
[{"f": "asc"}] or ["f"], one direction |
use_index |
Named design document or index; a warning if it cannot serve the query |
conflicts |
Adds _conflicts
|
(source: src/protocol/couchdb/api.rs, find)
Index choice: an index is usable when every indexed field is constrained by the selector and, when sorting, the sort fields are a prefix of the index fields in one direction. A sort that no index can serve, other than a sort on _id alone, is 400 no_usable_index, as in CouchDB (source: src/protocol/couchdb/api.rs, find).
Execution then scans every document of the database in memory. A chosen JSON index does not narrow the scan; its only effect is to exclude documents that lack one of its fields. Design documents are skipped (source: src/protocol/couchdb/api.rs, find). _explain returns the chosen index and options without running the query (source: src/protocol/couchdb/api.rs, find with explain).
Tested by collation_order, operators and projection in src/protocol/couchdb/mango.rs, mango_find_and_index in src/protocol/couchdb/mod.rs, and post_bulk_docs_all_docs_and_find in tests/protocol_couchdb.rs.
Views
-
Map. Map functions are JavaScript, run in an embedded QuickJS runtime from the
rquickjscrate, with a 30-second deadline per indexing run (MAP_TIMEOUT);emit(k, v)collects rows (source:src/protocol/couchdb/views.rs,new_runtime,MAP_TIMEOUT,refresh). -
Reduce. Built-ins
_count,_sum,_statsand_approx_count_distinct, or a custom JavaScript reduce (source:src/protocol/couchdb/views.rs,builtin_reduce,js_reduce). -
Index lifetime. A view index is built on the first query, kept in the database's
view_cache, and updated from the database sequence on later queries. It is not persisted and is rebuilt after a restart (source:src/protocol/couchdb/views.rs, module comment,refresh). - The same runtime runs JavaScript
_changesfilters (source:src/protocol/couchdb/views.rs,filter_fn).
Not verified: no Rust test runs a view, a reduce or a JavaScript filter; views are covered only by the CouchDB differential recorded in the proof pack (source: reference, Test coverage).
Storage and reachability from SQL
Documents are persisted to the SQL tables _couchdb_dbs and _couchdb_docs, one row per document holding the revision tree as JSON (source: src/protocol/couchdb/store.rs, CREATE_DBS, CREATE_DOCS, persist_docs). The _couchdb_ prefix makes these wire-internal tables, so a role that is not a superuser needs a GRANT to read them over SQL (source: src/query/engine.rs, is_wire_internal_relation). Mango and views are not accepted on any SQL path.
Not supported
$text-style search (_search, Nouveau), partitioned queries (_partition/...), persisted view indexes, and show, list, update and rewrite functions (source: reference, Not implemented).