1.3.6. /db/_find

POST /{db}/_find

Find documents using a declarative JSON querying syntax. Queries can use the built-in _all_docs index or custom indexes, specified using the _index endpoint.

Parameters:
  • db – Database name
Request Headers:
 
Request JSON Object:
 
  • selector (json) – JSON object describing criteria used to select documents. More information provided in the section on selector syntax. Required
  • limit (number) – Maximum number of results returned. Default is 25. Optional
  • skip (number) – Skip the first ‘n’ results, where ‘n’ is the value specified. Optional
  • sort (json) – JSON array following sort syntax. Optional
  • fields (array) – JSON array specifying which fields of each object should be returned. If it is omitted, the entire object is returned. More information provided in the section on filtering fields. Optional
  • use_index (string|array) – Instruct a query to use a specific index. Specified either as “<design_document>” or [“<design_document>”, “<index_name>”]. Optional
  • conflicts (boolean) – Include conflicted documents if true. Intended use is to easily find conflicted documents, without an index or view. Default is false. Optional
  • r (number) – Read quorum needed for the result. This defaults to 1, in which case the document found in the index is returned. If set to a higher value, each document is read from at least that many replicas before it is returned in the results. This is likely to take more time than using only the document stored locally with the index. Optional, default: 1
  • bookmark (string) – A string that enables you to specify which page of results you require. Used for paging through result sets. Every query returns an opaque string under the bookmark key that can then be passed back in a query to get the next page of results. If any part of the selector query changes between requests, the results are undefined. Optional, default: null
  • update (boolean) – Whether to update the index prior to returning the result. Default is true. Optional
  • stable (boolean) – Whether or not the view results should be returned from a “stable” set of shards. Optional
  • stale (string) – Combination of update=false and stable=true options. Possible options: “ok”, false (default). Optional Note that this parameter is deprecated. Use stable and update instead. See Views Generation for more details.
  • execution_stats (boolean) – Include execution statistics in the query response. Optional, default: false
Response Headers:
 
Response JSON Object:
 
  • docs (object) – Array of documents matching the search. In each matching document, the fields specified in the fields part of the request body are listed, along with their values.
  • warning (string) – Execution warnings
  • execution_stats (object) – Execution statistics
  • bookmark (string) – An opaque string used for paging. See the bookmark field in the request (above) for usage details.
Status Codes:

The limit and skip values are exactly as you would expect. While skip exists, it is not intended to be used for paging. The reason is that the bookmark feature is more efficient.

Request:

Example request body for finding documents using an index:

  1. POST /movies/_find HTTP/1.1
  2. Accept: application/json
  3. Content-Type: application/json
  4. Content-Length: 168
  5. Host: localhost:5984
  6. {
  7. "selector": {
  8. "year": {"$gt": 2010}
  9. },
  10. "fields": ["_id", "_rev", "year", "title"],
  11. "sort": [{"year": "asc"}],
  12. "limit": 2,
  13. "skip": 0,
  14. "execution_stats": true
  15. }

Response:

Example response when finding documents using an index:

  1. HTTP/1.1 200 OK
  2. Cache-Control: must-revalidate
  3. Content-Type: application/json
  4. Date: Thu, 01 Sep 2016 15:41:53 GMT
  5. Server: CouchDB (Erlang OTP)
  6. Transfer-Encoding: chunked
  7. {
  8. "docs": [
  9. {
  10. "_id": "176694",
  11. "_rev": "1-54f8e950cc338d2385d9b0cda2fd918e",
  12. "year": 2011,
  13. "title": "The Tragedy of Man"
  14. },
  15. {
  16. "_id": "780504",
  17. "_rev": "1-5f14bab1a1e9ac3ebdf85905f47fb084",
  18. "year": 2011,
  19. "title": "Drive"
  20. }
  21. ],
  22. "execution_stats": {
  23. "total_keys_examined": 0,
  24. "total_docs_examined": 200,
  25. "total_quorum_docs_examined": 0,
  26. "results_returned": 2,
  27. "execution_time_ms": 5.52
  28. }
  29. }

1.3.6.1. Selector Syntax

Selectors are expressed as a JSON object describing documents of interest. Within this structure, you can apply conditional logic using specially named fields.

Whilst selectors have some similarities with MongoDB query documents, these arise from a similarity of purpose and do not necessarily extend to commonality of function or result.

1.3.6.1.1. Selector Basics

Elementary selector syntax requires you to specify one or more fields, and the corresponding values required for those fields. This selector matches all documents whose “director” field has the value “Lars von Trier”.

  1. {
  2. "director": "Lars von Trier"
  3. }
  4. A simple selector, inspecting specific fields
  1. "selector": {
  2. "title": "Live And Let Die"
  3. },
  4. "fields": [
  5. "title",
  6. "cast"
  7. ]

You can create more complex selector expressions by combining operators. For best performance, it is best to combine ‘combination’ or ‘array logical’ operators, such as $regex, with an equality operators such as $eq, $gt, $gte, $lt, and $lte (but not $ne). For more information about creating complex selector expressions, see creating selector expressions.

1.3.6.1.2. Selector with 2 fields

This selector matches any document with a name field containing "Paul", and that also has a location field with the value "Boston".

  1. {
  2. "name": "Paul",
  3. "location": "Boston"
  4. }

1.3.6.1.3. Subfields

A more complex selector enables you to specify the values for field of nested objects, or subfields. For example, you might use a standard JSON structure for specifying a field and subfield.

Example of a field and subfield selector, using a standard JSON structure:

  1. {
  2. "imdb": {
  3. "rating": 8
  4. }
  5. }

An abbreviated equivalent uses a dot notation to combine the field and subfield names into a single name.

  1. {
  2. "imdb.rating": 8
  3. }

1.3.6.1.4. Operators

Operators are identified by the use of a dollar sign ($) prefix in the name field.

There are two core types of operators in the selector syntax:

  • Combination operators
  • Condition operators

In general, combination operators are applied at the topmost level of selection. They are used to combine conditions, or to create combinations of conditions, into one selector.

Every explicit operator has the form:

  1. {"$operator": argument}

A selector without an explicit operator is considered to have an implicit operator. The exact implicit operator is determined by the structure of the selector expression.

1.3.6.1.5. Implicit Operators

There are two implicit operators:

  • Equality
  • And

In a selector, any field containing a JSON value, but that has no operators in it, is considered to be an equality condition. The implicit equality test applies also for fields and subfields.

Any JSON object that is not the argument to a condition operator is an implicit $and operator on each field.

In the below example, we use an operator to match any document, where the "year" field has a value greater than 2010:

  1. {
  2. "year": {
  3. "$gt": 2010
  4. }
  5. }

In this next example, there must be a field "director" in a matching document, and the field must have a value exactly equal to "Lars von Trier".

  1. {
  2. "director": "Lars von Trier"
  3. }

You can also make the equality operator explicit.

  1. {
  2. "director": {
  3. "$eq": "Lars von Trier"
  4. }
  5. }

In the next example using subfields, the required field "imdb" in a matching document must also have a subfield "rating" and the subfield must have a value equal to 8.

Example of implicit operator applied to a subfield test

  1. {
  2. "imdb": {
  3. "rating": 8
  4. }
  5. }

Again, you can make the equality operator explicit.

  1. {
  2. "imdb": {
  3. "rating": { "$eq": 8 }
  4. }
  5. }

An example of the $eq operator used with full text indexing

  1. {
  2. "selector": {
  3. "year": {
  4. "$eq": 2001
  5. }
  6. },
  7. "sort": [
  8. "title:string"
  9. ],
  10. "fields": [
  11. "title"
  12. ]
  13. }

An example of the $eq operator used with database indexed on the field "year"

  1. {
  2. "selector": {
  3. "year": {
  4. "$eq": 2001
  5. }
  6. },
  7. "sort": [
  8. "year"
  9. ],
  10. "fields": [
  11. "year"
  12. ]
  13. }

In this example, the field "director" must be present and contain the value "Lars von Trier" and the field "year" must exist and have the value 2003.

  1. {
  2. "director": "Lars von Trier",
  3. "year": 2003
  4. }

You can make both the $and operator and the equality operator explicit.

Example of using explicit $and and $eq operators

  1. {
  2. "$and": [
  3. {
  4. "director": {
  5. "$eq": "Lars von Trier"
  6. }
  7. },
  8. {
  9. "year": {
  10. "$eq": 2003
  11. }
  12. }
  13. ]
  14. }

1.3.6.1.6. Explicit Operators

All operators, apart from ‘Equality’ and ‘And’, must be stated explicitly.

1.3.6.1.7. Combination Operators

Combination operators are used to combine selectors. In addition to the common boolean operators found in most programming languages, there are three combination operators ($all, $elemMatch, and $allMatch) that help you work with JSON arrays and one that works with JSON maps ($keyMapMatch).

A combination operator takes a single argument. The argument is either another selector, or an array of selectors.

The list of combination operators:

OperatorArgumentPurpose
$andArrayMatches if all the selectors in the array match.
$orArrayMatches if any of the selectors in the array match. All selectors must use the same index.
$notSelectorMatches if the given selector does not match.
$norArrayMatches if none of the selectors in the array match.
$allArrayMatches an array value if it contains all the elements of the argument array.
$elemMatchSelectorMatches and returns all documents that contain an array field with at least one element that matches all the specified query criteria.
$allMatchSelectorMatches and returns all documents that contain an array field with all its elements matching all the specified query criteria.
$keyMapMatchSelectorMatches and returns all documents that contain a map that contains at least one key that matches all the specified query criteria.

The $and operator

$and operator used with two fields

  1. {
  2. "selector": {
  3. "$and": [
  4. {
  5. "title": "Total Recall"
  6. },
  7. {
  8. "year": {
  9. "$in": [1984, 1991]
  10. }
  11. }
  12. ]
  13. },
  14. "fields": [
  15. "year",
  16. "title",
  17. "cast"
  18. ]
  19. }

The $and operator matches if all the selectors in the array match. Below is an example using the primary index (_all_docs):

  1. {
  2. "$and": [
  3. {
  4. "_id": { "$gt": null }
  5. },
  6. {
  7. "year": {
  8. "$in": [2014, 2015]
  9. }
  10. }
  11. ]
  12. }

The $or operator

The $or operator matches if any of the selectors in the array match. Below is an example used with an index on the field "year":

  1. {
  2. "year": 1977,
  3. "$or": [
  4. { "director": "George Lucas" },
  5. { "director": "Steven Spielberg" }
  6. ]
  7. }

The $not operator

The $not operator matches if the given selector does not match. Below is an example used with an index on the field "year":

  1. {
  2. "year": {
  3. "$gte": 1900
  4. },
  5. "year": {
  6. "$lte": 1903
  7. },
  8. "$not": {
  9. "year": 1901
  10. }
  11. }

The $nor operator

The $nor operator matches if the given selector does not match. Below is an example used with an index on the field "year":

  1. {
  2. "year": {
  3. "$gte": 1900
  4. },
  5. "year": {
  6. "$lte": 1910
  7. },
  8. "$nor": [
  9. { "year": 1901 },
  10. { "year": 1905 },
  11. { "year": 1907 }
  12. ]
  13. }

The $all operator

The $all operator matches an array value if it contains all the elements of the argument array. Below is an example used with the primary index (_all_docs):

  1. {
  2. "_id": {
  3. "$gt": null
  4. },
  5. "genre": {
  6. "$all": ["Comedy","Short"]
  7. }
  8. }

The $elemMatch operator

The $elemMatch operator matches and returns all documents that contain an array field with at least one element matching the supplied query criteria. Below is an example used with the primary index (_all_docs):

  1. {
  2. "_id": { "$gt": null },
  3. "genre": {
  4. "$elemMatch": {
  5. "$eq": "Horror"
  6. }
  7. }
  8. }

The $allMatch operator

The $allMatch operator matches and returns all documents that contain an array field with all its elements matching the supplied query criteria. Below is an example used with the primary index (_all_docs):

  1. {
  2. "_id": { "$gt": null },
  3. "genre": {
  4. "$allMatch": {
  5. "$eq": "Horror"
  6. }
  7. }
  8. }

The $keyMapMatch operator

The $keyMapMatch operator matches and returns all documents that contain a map that contains at least one key that matches all the specified query criteria. Below is an example used with the primary index (_all_docs):

  1. {
  2. "_id": { "$gt": null },
  3. "cameras": {
  4. "$keyMapMatch": {
  5. "$eq": "secondary"
  6. }
  7. }
  8. }

1.3.6.1.8. Condition Operators

Condition operators are specific to a field, and are used to evaluate the value stored in that field. For instance, the basic $eq operator matches when the specified field contains a value that is equal to the supplied argument.

The basic equality and inequality operators common to most programming languages are supported. Strict type matching is used.

In addition, some ‘meta’ condition operators are available. Some condition operators accept any valid JSON content as the argument. Other condition operators require the argument to be in a specific JSON format.

Operator typeOperatorArgumentPurpose
(In)equality$ltAny JSONThe field is less than the argument
 $lteAny JSONThe field is less than or equal to the argument.
 $eqAny JSONThe field is equal to the argument
 $neAny JSONThe field is not equal to the argument.
 $gteAny JSONThe field is greater than or equal to the argument.
 $gtAny JSONThe field is greater than the to the argument.
Object$existsBooleanCheck whether the field exists or not, regardless of its value.
 $typeStringCheck the document field’s type. Valid values are “null”, “boolean”, “number”, “string”, “array”, and “object”.
Array$inArray of JSON valuesThe document field must exist in the list provided.
 $ninArray of JSON valuesThe document field not must exist in the list provided.
 $sizeIntegerSpecial condition to match the length of an array field in a document. Non-array fields cannot match this condition.
Miscellaneous$mod[Divisor, Remainder]Divisor and Remainder are both positive or negative integers. Non-integer values result in a 404. Matches documents where field % Divisor == Remainder is true, and only when the document field is an integer.
 $regexStringA regular expression pattern to match against the document field. Only matches when the field is a string value and matches the supplied regular expression. The matching algorithms are based on the Perl Compatible Regular Expression (PCRE) library. For more information about what is implemented, see the see the Erlang Regular Expression

1.3.6.1.9. Creating Selector Expressions

We have seen examples of combining selector expressions, such as using explicit $and and $eq operators.

In general, whenever you have an operator that takes an argument, that argument can itself be another operator with arguments of its own. This enables us to build up more complex selector expressions.

However, only equality operators such as $eq, $gt, $gte, $lt, and $lte (but not $ne) can be used as the basis of a query. You should include at least one of these in a selector.

For example, if you try to perform a query that attempts to match all documents that have a field called afieldname containing a value that begins with the letter A, this will trigger a warning because no index could be used and the database performs a full scan of the primary index:

Request

  1. POST /movies/_find HTTP/1.1
  2. Accept: application/json
  3. Content-Type: application/json
  4. Content-Length: 112
  5. Host: localhost:5984
  6. {
  7. "selector": {
  8. "afieldname": {"$regex": "^A"}
  9. }
  10. }

Response:

  1. HTTP/1.1 200 OK
  2. Cache-Control: must-revalidate
  3. Content-Type: application/json
  4. Date: Thu, 01 Sep 2016 17:25:51 GMT
  5. Server: CouchDB (Erlang OTP)
  6. Transfer-Encoding: chunked
  7. {
  8. "warning":"no matching index found, create an index to optimize
  9. query time",
  10. "docs":[
  11. ]
  12. }

Most selector expressions work exactly as you would expect for the given operator. But it is not always the case: for example, comparison of strings is done with ICU and can can give surprising results if you were expecting ASCII ordering. See Views Collation for more details.

1.3.6.2. Sort Syntax

The sort field contains a list of field name and direction pairs, expressed as a basic array. The first field name and direction pair is the topmost level of sort. The second pair, if provided, is the next level of sort.

The field can be any field, using dotted notation if desired for sub-document fields.

The direction value is "asc" for ascending, and "desc" for descending. If you omit the direction value, the default "asc" is used.

Example, sorting by 2 fields:

  1. [{"fieldName1": "desc"}, {"fieldName2": "desc" }]

Example, sorting by 2 fields, assuming default direction for both :

  1. ["fieldNameA", "fieldNameB"]

A typical requirement is to search for some content using a selector, then to sort the results according to the specified field, in the required direction.

To use sorting, ensure that:

  • At least one of the sort fields is included in the selector.
  • There is an index already defined, with all the sort fields in the same

    order.

  • Each object in the sort array has a single key.

If an object in the sort array does not have a single key, the resulting sort order is implementation specific and might change.

Find does not support multiple fields with different sort orders, so the directions must be either all ascending or all descending.

For field names in text search sorts, it is sometimes necessary for a field type to be specified, for example:

{ "<fieldname>:string": "asc"}

If possible, an attempt is made to discover the field type based on the selector. In ambiguous cases the field type must be provided explicitly.

The sorting order is undefined when fields contain different data types. This is an important difference between text and view indexes. Sorting behavior for fields with different data types might change in future versions.

A simple query, using sorting:

  1. {
  2. "selector": {"Actor_name": "Robert De Niro"},
  3. "sort": [{"Actor_name": "asc"}, {"Movie_runtime": "asc"}]
  4. }

1.3.6.3. Filtering Fields

It is possible to specify exactly which fields are returned for a document when selecting from a database. The two advantages are:

  • Your results are limited to only those parts of the document that are

    required for your application.

  • A reduction in the size of the response.

The fields returned are specified as an array.

Only the specified filter fields are included, in the response. There is no automatic inclusion of the _id or other metadata fields when a field list is included.

Example of selective retrieval of fields from matching documents:

  1. {
  2. "selector": { "Actor_name": "Robert De Niro" },
  3. "fields": ["Actor_name", "Movie_year", "_id", "_rev"]
  4. }

1.3.6.4. Pagination

Mango queries support pagination via the bookmark field. Every _find response contains a bookmark - a token that CouchDB uses to determine where to resume from when subsequent queries are made. To get the next set of query results, add the bookmark that was received in the previous response to your next request. Remember to keep the selector the same, otherwise you will receive unexpected results. To paginate backwards, you can use a previous bookmark to return the previous set of results.

Note that the presence of a bookmark doesn’t guarantee that there are more results. You can to test whether you have reached the end of the result set by comparing the number of results returned with the page size requested - if results returned < limit, there are no more.

1.3.6.5. Execution Statistics

Find can return basic execution statistics for a specific request. Combined with the _explain endpoint, this should provide some insight as to whether indexes are being used effectively.

The execution statistics currently include:

FieldDescription
total_keys_examinedNumber of index keys examined. Currently always 0.
total_docs_examinedNumber of documents fetched from the database / index, equivalent to using include_docs=true in a view. These may then be filtered in-memory to further narrow down the result set based on the selector.
total_quorum_docs_examinedNumber of documents fetched from the database using an out-of-band document fetch. This is only non-zero when read quorum > 1 is specified in the query parameters.
results_returnedNumber of results returned from the query. Ideally this should not be significantly lower than the total documents / keys examined.
execution_time_msTotal execution time in milliseconds as measured by the database.

1.3.7. /db/_index

Mango is a declarative JSON querying language for CouchDB databases. Mango wraps several index types, starting with the Primary Index out-of-the-box. Mango indexes, with index type json, are built using MapReduce Views.

POST /{db}/_index

Create a new index on a database

Parameters:
  • db – Database name
Request Headers:
 
Query Parameters:
 
  • index (json) – JSON object describing the index to create.
  • ddoc (string) – Name of the design document in which the index will be created. By default, each index will be created in its own design document. Indexes can be grouped into design documents for efficiency. However, a change to one index in a design document will invalidate all other indexes in the same document (similar to views). Optional
  • name (string) – Name of the index. If no name is provided, a name will be generated automatically. Optional
  • type (string) – Can be “json” or “text”. Defaults to json. Geospatial indexes will be supported in the future. Optional Text indexes are supported via a third party library Optional
  • partitioned (boolean) – Determines whether a JSON index is partitioned or global. The default value of partitioned is the partitioned property of the database. To create a global index on a partitioned database, specify false for the “partitioned” field. If you specify true for the “partitioned” field on an unpartitioned database, an error occurs.
Response Headers:
 
Response JSON Object:
 
  • result (string) – Flag to show whether the index was created or one already exists. Can be “created” or “exists”.
  • id (string) – Id of the design document the index was created in.
  • name (string) – Name of the index created.
Status Codes:

The Index object is a JSON object with the following fields:

JSON Object:
  • fields (array) – array of field names following the sort syntax. Nested fields are also allowed, e.g. “person.name”.
  • partial_filter_selector (json) – A selector to apply to documents at indexing time, creating a partial index. Optional

Example of creating a new index for a field called foo:

Request:

  1. POST /db/_index HTTP/1.1
  2. Content-Type: application/json
  3. Content-Length: 116
  4. Host: localhost:5984
  5. {
  6. "index": {
  7. "fields": ["foo"]
  8. },
  9. "name" : "foo-index",
  10. "type" : "json"
  11. }

The returned JSON confirms the index has been created:

Response:

  1. HTTP/1.1 200 OK
  2. Cache-Control: must-revalidate
  3. Content-Length: 96
  4. Content-Type: application/json
  5. Date: Thu, 01 Sep 2016 18:17:48 GMT
  6. Server: CouchDB (Erlang OTP/18)
  7. {
  8. "result":"created",
  9. "id":"_design/a5f4711fc9448864a13c81dc71e660b524d7410c",
  10. "name":"foo-index"
  11. }

Example index creation using all available query parameters

Request:

  1. POST /db/_index HTTP/1.1
  2. Content-Type: application/json
  3. Content-Length: 396
  4. Host: localhost:5984
  5. {
  6. "index": {
  7. "partial_filter_selector": {
  8. "year": {
  9. "$gt": 2010
  10. },
  11. "limit": 10,
  12. "skip": 0
  13. },
  14. "fields": [
  15. "_id",
  16. "_rev",
  17. "year",
  18. "title"
  19. ]
  20. },
  21. "ddoc": "example-ddoc",
  22. "name": "example-index",
  23. "type": "json",
  24. "partitioned": false
  25. }

By default, a JSON index will include all documents that have the indexed fields present, including those which have null values.

1.3.7.1. Partial Indexes

Partial indexes allow documents to be filtered at indexing time, potentially offering significant performance improvements for query selectors that don’t map cleanly to a range query on an index.

Let’s look at an example query:

  1. {
  2. "selector": {
  3. "status": {
  4. "$ne": "archived"
  5. },
  6. "type": "user"
  7. }
  8. }

Without a partial index, this requires a full index scan to find all the documents of "type":"user" that do not have a status of "archived". This is because a normal index can only be used to match contiguous rows, and the "$ne" operator cannot guarantee that.

To improve response times, we can create an index which excludes documents where "status": { "$ne": "archived" } at index time using the "partial_filter_selector" field:

  1. POST /db/_index HTTP/1.1
  2. Content-Type: application/json
  3. Content-Length: 144
  4. Host: localhost:5984
  5. {
  6. "index": {
  7. "partial_filter_selector": {
  8. "status": {
  9. "$ne": "archived"
  10. }
  11. },
  12. "fields": ["type"]
  13. },
  14. "ddoc" : "type-not-archived",
  15. "type" : "json"
  16. }

Partial indexes are not currently used by the query planner unless specified by a "use_index" field, so we need to modify the original query:

  1. {
  2. "selector": {
  3. "status": {
  4. "$ne": "archived"
  5. },
  6. "type": "user"
  7. },
  8. "use_index": "type-not-archived"
  9. }

Technically, we don’t need to include the filter on the "status" field in the query selector - the partial index ensures this is always true - but including it makes the intent of the selector clearer and will make it easier to take advantage of future improvements to query planning (e.g. automatic selection of partial indexes).

GET /{db}/_index

When you make a GET request to /db/_index, you get a list of all indexes in the database. In addition to the information available through this API, indexes are also stored in design documents <index-functions>. Design documents are regular documents that have an ID starting with _design/. Design documents can be retrieved and modified in the same way as any other document, although this is not necessary when using Mango.

Parameters:
  • db – Database name.
Response Headers:
 
Response JSON Object:
 
  • total_rows (number) – Number of indexes
  • indexes (object) – Array of index definitions
Status Codes:
  • Format of index objects:

    • ddoc: ID of the design document the index belongs to. This ID

      can be used to retrieve the design document containing the index, by making a GET request to /db/ddoc, where ddoc is the value of this field.

    • name: Name of the index.

    • type: Type of the index. Currently “json” is the only

      supported type.

    • def: Definition of the index, containing the indexed fields

      and the sort order: ascending or descending.

Request:

  1. GET /db/_index HTTP/1.1
  2. Accept: application/json
  3. Host: localhost:5984

Response:

  1. HTTP/1.1 200 OK
  2. Cache-Control: must-revalidate
  3. Content-Length: 238
  4. Content-Type: application/json
  5. Date: Thu, 01 Sep 2016 18:17:48 GMT
  6. Server: CouchDB (Erlang OTP/18)
  7. {
  8. "total_rows": 2,
  9. "indexes": [
  10. {
  11. "ddoc": null,
  12. "name": "_all_docs",
  13. "type": "special",
  14. "def": {
  15. "fields": [
  16. {
  17. "_id": "asc"
  18. }
  19. ]
  20. }
  21. },
  22. {
  23. "ddoc": "_design/a5f4711fc9448864a13c81dc71e660b524d7410c",
  24. "name": "foo-index",
  25. "type": "json",
  26. "def": {
  27. "fields": [
  28. {
  29. "foo": "asc"
  30. }
  31. ]
  32. }
  33. }
  34. ]
  35. }

DELETE /{db}/_index/{designdoc}/json/{name}

Parameters:
  • db – Database name.
  • designdoc – Design document name.
  • name – Index name.
Response Headers:
 
Response JSON Object:
 
  • ok (string) – “true” if successful.
Status Codes:

Request:

  1. DELETE /db/_index/_design/a5f4711fc9448864a13c81dc71e660b524d7410c/json/foo-index HTTP/1.1
  2. Accept: */*
  3. Host: localhost:5984

Response:

  1. HTTP/1.1 200 OK
  2. Cache-Control: must-revalidate
  3. Content-Length: 12
  4. Content-Type: application/json
  5. Date: Thu, 01 Sep 2016 19:21:40 GMT
  6. Server: CouchDB (Erlang OTP/18)
  7. {
  8. "ok": true
  9. }

1.3.8. /db/_explain

POST /{db}/_explain

Shows which index is being used by the query. Parameters are the same as _find

Parameters:
  • db – Database name
Request Headers:
 
Response Headers:
 
Response JSON Object:
 
  • dbname (string) – Name of database
  • index (object) – Index used to fulfill the query
  • selector (object) – Query selector used
  • opts (object) – Query options used
  • limit (number) – Limit parameter used
  • skip (number) – Skip parameter used
  • fields (array) – Fields to be returned by the query
  • range (object) – Range parameters passed to the underlying view
Status Codes:

Request:

  1. POST /movies/_explain HTTP/1.1
  2. Accept: application/json
  3. Content-Type: application/json
  4. Content-Length: 168
  5. Host: localhost:5984
  6. {
  7. "selector": {
  8. "year": {"$gt": 2010}
  9. },
  10. "fields": ["_id", "_rev", "year", "title"],
  11. "sort": [{"year": "asc"}],
  12. "limit": 2,
  13. "skip": 0
  14. }

Response:

  1. HTTP/1.1 200 OK
  2. Cache-Control: must-revalidate
  3. Content-Type: application/json
  4. Date: Thu, 01 Sep 2016 15:41:53 GMT
  5. Server: CouchDB (Erlang OTP)
  6. Transfer-Encoding: chunked
  7. {
  8. "dbname": "movies",
  9. "index": {
  10. "ddoc": "_design/0d61d9177426b1e2aa8d0fe732ec6e506f5d443c",
  11. "name": "0d61d9177426b1e2aa8d0fe732ec6e506f5d443c",
  12. "type": "json",
  13. "def": {
  14. "fields": [
  15. {
  16. "year": "asc"
  17. }
  18. ]
  19. }
  20. },
  21. "selector": {
  22. "year": {
  23. "$gt": 2010
  24. }
  25. },
  26. "opts": {
  27. "use_index": [],
  28. "bookmark": "nil",
  29. "limit": 2,
  30. "skip": 0,
  31. "sort": {},
  32. "fields": [
  33. "_id",
  34. "_rev",
  35. "year",
  36. "title"
  37. ],
  38. "r": [
  39. 49
  40. ],
  41. "conflicts": false
  42. },
  43. "limit": 2,
  44. "skip": 0,
  45. "fields": [
  46. "_id",
  47. "_rev",
  48. "year",
  49. "title"
  50. ],
  51. "range": {
  52. "start_key": [
  53. 2010
  54. ],
  55. "end_key": [
  56. {}
  57. ]
  58. }
  59. }

1.3.8.1. Index selection

_find chooses which index to use for responding to a query, unless you specify an index at query time.

The query planner looks at the selector section and finds the index with the closest match to operators and fields used in the query. If there are two or more json type indexes that match, the index with the smallest number of fields in the index is preferred. If there are still two or more candidate indexes, the index with the first alphabetical name is chosen.