Eventually this will replace PouchDB's map/reduce API entirely. You'll still be able to use map/reduce, but it will be distributed as a separate plugin.
Warning: this is beta software! It may change at anytime and could be unstable.
$elemMatch, multi-field queries, multi-field indexes, multi-field sort,
'deep.fields.like.this', ascending and descending sort.
ddoc when creating an index
0.8.0: Bug fixes for $and, $type, $exists
0.9.0: Bug fixes for
$elemMatch. Rewrite of in-memory operator
0.10.0: Update for latest Mango spec (warnings instead of errors), update for PouchDB 5.4.0.
To use this plugin in the browser, include it after
pouchdb.js in your HTML page:
You can also download it from Bower:
bower install pouchdb-find
Or to use it in Node.js, just npm install it:
npm install pouchdb-find
And then attach it to the
var PouchDB = ;PouchDB;
This API is modeled after the Cloudant query API, merged into CouchDB 2.0. Read that page for more details.
As with PouchDB, the entire API accepts either the callback or the Promise style.
db.createIndex(index [, callback])
db.deleteIndex(index [, callback])
db.find(request [, callback])
Create an index if it doesn't exist, or do nothing if it already exists.
The result can be either:
"result": "created" // index was created
"result": "exists" // index already exists
You can also create an index on multiple fields:
Or an index on deep fields:
You can also specify additional options, if you want more control over how your index is created:
fieldsis a list of fields to index
name(optional) name of the index, auto-generated if you don't include it
ddoc(optional) design document name (i.e. the part after
'_design/', auto-generated if you don't include it
type(optional) only supports
'json', and it's also the default
Get a list of all the indexes you've created. Also tells you about the special
_all_docs index, i.e. the default index on the
"indexes":"ddoc": null"name": "_all_docs""type": "special""def":"fields":"_id": "asc""ddoc": "_design/idx-0f3a6f73110868266fa5c688caf8acd3""name": "idx-0f3a6f73110868266fa5c688caf8acd3""type": "json""def":"fields":"foo": "asc""bar": "asc"
Delete an index and clean up any leftover data on the disk.
indexDefinition of an index to delete. You can pass it in exactly as you received it from the
getIndexes()API. You cannot delete the built-in
Notice that you don't need to provide a
_rev! The design doc is also deleted.
Query the API to find some documents.
"docs":"_id": "mario""name": "Mario"
selector Defines a selector to filter the results. Required.
$ltMatch fields "less than" this one.
$gtMatch fields "greater than" this one.
$lteMatch fields "less than or equal to" this one.
$gteMatch fields "greater than or equal to" this one.
$eqMatch fields equal to this one.
$neMatch fields not equal to this one.
$existsTrue if the field should exist, false otherwise.
$typeOne of: "null", "boolean", "number", "string", "array", or "object".
$inMatches if all the selectors in the array match.
$andMatches if all the selectors in the array match.
$ninThe document field must not exist in the list provided.
$allMatches an array value if it contains all the elements of the argument array.
$sizeSpecial condition to match the length of an array field in a document.
$orMatches if any of the selectors in the array match. All selectors must use the same index.
$norMatches if none of the selectors in the array match.
$notMatches if the given selector does not match.
$modMatches documents where (field % Divisor == Remainder) is true, and only when the document field is an integer.
$regexA regular expression pattern to match against the document field.
$elemMatchMatches all documents that contain an array field with at least one element that matches all the specified query criteria.
fields (Optional) Defines a list of fields that you want to receive. If omitted, you get the full documents.
sort (Optional) Defines a list of fields defining how you want to sort. Note that sorted fields also have to be selected in the
limit (Optional) Maximum number of documents to return.
skip (Optional) Number of docs to skip before returning.
If there's no index that matches your
sort, then this method will issue a warning.
The best index will be chosen automatically. If you want to see the query plan for your query, then turn on debugging.
See the Cloudant docs for more details.
Find all docs where
doc.name === 'Mario':
This is equivalent to:
Find all docs where
doc.series === 'Mario' and
doc.debut > 1990:
This is equivalent to:
Return all docs sorted by
For more examples, refer to Cloudant's
Over HTTP, this plugin currently works with Cloudant and CouchDB 2.0. Cloudant is the reference implementation, so the API should be the same.
PouchDB Server also has this API, since it includes this very plugin by default.
pouchdb-find will start logging some debug information to the console. This can be useful if, for instance, you want to see the query plan that is being used to execute your queries.
Thanks very much to @garrensmith for implementing all the new features from 0.4.0 to 0.6.0!
Instructions are in CONTRIBUTING.md.