v0.7.0
Menu

#Filters

A filter is a document. It is used by find, count, distinct, update, delete, replace, findOneAndUpdate, findOneAndDelete and the $match stage of aggregation. An empty filter {} matches every document, and so does leaving the filter out of find, count and distinct.

Every top-level entry of a filter has to hold for a document to match. A filter is checked for bad operators before anything is looked at, so a bad shape is an error even on an empty collection.

{"cmd":"find","collection":"users","filter":{"age":{"$gt":18},"address.city":"Leeds"}}

#Paths

A key is a field name, or a path into nested documents with dots: "address.city" is the city field of the address document. Paths work in filters, updates, sort keys, projections and index definitions.

#Comparison and set operators

A plain value means equality: {"age":25}. Operators go in a document under the field:

Operator Matches when
$eq the field equals the value
$ne the field does not equal the value. A missing field passes
$gt, $gte, $lt, $lte the field is above, above or equal, below, below or equal. Only values of the same type compare, so a number range never returns a string
$in the field equals any value of the list. An item can be a {"$regex":...} document
$nin the opposite of $in. A missing field passes
$exists true for a field that is there (null counts), false for one that is missing. Takes a boolean
$not the operators inside it are not all met, as in {"age":{"$not":{"$gt":5}}}. Takes operators only, can nest. A missing field passes
$mod [divisor, remainder], for whole numbers. A float is cut to its whole part first, and the sign follows the number, so -7 % 3 is -1. The divisor cannot be 0
$type the type name, a list of names, or a number. See below
$regex a string field matches the pattern. Takes $options of i, m, s and x

Equality between documents and arrays goes into them, so [1] equals [1.0]. $gte and $lte also match a value that is equal but has no order, such as true, null, an id, a document or an array.

#$type

The names are null, bool, int, long (same as int), double, number (either), string, objectId, date, regex, array and object. An int is a 64 bit one. MongoDB type numbers are also read: 1 double, 2 string, 3 object, 4 array, 7 objectId, 8 bool, 9 date, 10 null, 11 regex, 16 and 18 int. An unknown name is an error.

{"cmd":"find","collection":"users","filter":{"tags":{"$type":"string"}}}

#$regex

{"cmd":"find","collection":"users","filter":{"name":{"$regex":"^d","$options":"i"}}}

It matches anywhere in the string unless anchored, and tries each element of an array. It does not match values that are not text or are missing. The engine is Rust's regex crate, which matches in time proportional to the text so no pattern can hang the server. It has no backreferences or lookaround, which MongoDB's PCRE has. A pattern is limited to 4096 bytes and a compiled size of 1 MiB. $options without $regex is an error.

A stored regex value, written {"$regularExpression":{...}}, is matched as a pattern against strings, and equals another stored regex with the same pattern and options.

#Logical operators

$and, $or and $nor take a non-empty list of filters:

{"cmd":"find","collection":"users","filter":{"$or":[{"age":{"$lt":18}},{"address.city":"York"}]}}

$nor matches when none of the filters match.

#Arrays

When a field holds an array, a condition is also tried on each element, one level down. The array as a whole is tried too.

  • {"tags":"red"} finds ["red","blue"].
  • {"scores":{"$gt":5}} finds [1,9], because one element is enough.
  • {"tags":["red","blue"]} is equality of the whole array.
  • Several operators on one field can be met by different elements, so {"scores":{"$gt":5,"$lt":2}} finds [1,9].
  • $ne and $nin mean no element matches.
  • A path can name a position, "scores.0", and "items.price" is the price of every item.
Operator Matches when
$all every listed value is found among the elements. An empty list matches nothing
$size the array has that many elements. Takes a whole number, 0 or more
$elemMatch one element meets all the conditions

$elemMatch takes operators for plain elements, or a filter for elements that are documents:

{"cmd":"find","collection":"orders","filter":{"scores":{"$elemMatch":{"$gt":3,"$lt":5}}}}
{"cmd":"find","collection":"orders","filter":{"items":{"$elemMatch":{"k":"y","q":{"$gt":5}}}}}

#Sort, skip and limit

These are fields of find, next to filter.

Field Meaning
sort {"age":-1} for one field, or [["age",-1],["name",1]] for several. 1 is ascending, -1 descending
skip how many matches to pass over. A whole number, 0 or more
limit how many to return. At least 1: leave it out for no limit
projection which fields come back, see below
{"cmd":"find","collection":"users","filter":{"age":{"$gte":1}},"sort":[["age",-1],["name",1]],"skip":1,"limit":1}

Several sort fields have to be a list, because the fields of a document have no order and a JSON object would lose the order of its keys.

  • limit also works on update and delete. skip and sort are for find only (and for the one-document commands, which take sort).
  • Values sort in this order: null (a missing field counts as null), numbers, strings, documents, arrays, ids, then bools. A date sorts after bools. NaN comes before every other number.
  • An array sorts by its smallest element going up and its largest going down. An empty array comes before null going up.
  • Equal values go by _id, so pages made with skip and limit do not overlap or skip documents as long as nothing changes between requests.
  • Without limit, a find returns every match, up to the reply size limit.
  • Without a sort, which documents a limit picks is not defined. For update and delete that means some matching document, like MongoDB's updateOne. Sort with find first if it matters.

A sort with a limit on one indexed field is read straight off the index. See Indexes.

#Projection

projection limits which fields of each document come back. It runs last, so the filter and sort see the whole document, and a sort on a field that is not in the projection still works. get takes it too.

{"cmd":"find","collection":"users","filter":{},"projection":{"name":1,"_id":0}}
Form Meaning
{"name":1,"address.city":1} leave in: those fields and _id
{"password":0} leave out: everything but those fields
{"name":1,"_id":0} leave in, without _id. The only mixing that is allowed
{} or none the whole document

The values are 1, 0, true or false. Any other number counts as 1 or 0 by whether it is zero.

  • A path goes into documents, and into the documents of an array one by one: {"orders.price":1} gives each order with only its price.
  • {"tags":{"$slice":3}} is the first 3 elements, -3 the last 3, 0 none, and [skip, n] is n elements after the first skip. It can go with either kind of projection, and cannot be on _id.
  • {"items":{"$elemMatch":{"k":"x"}}} gives the first element that matches, and drops the field when none do. It cannot be on a dotted path or on _id.
  • {"items.$":1} gives the first element that the filter matched. It works on find only, and the filter has to name the array directly or inside an $and.
  • Mixing leave-in and leave-out fields, a path that clashes with another ("a" with "a.b"), an empty path part, or a name that starts with $ is an error, reported before anything runs.

Not supported: $meta.

#count

{"cmd":"count","collection":"users","filter":{"address.city":"Leeds"}}

The reply is {"count":2,"ok":true}. With no filter, count is the size of the collection and nothing is scanned.

#Indexes

An index can answer equality, $in and range filters. $ne, $nin, $not, $exists, $mod, $type, $regex, $or and the array operators are scans unless another condition on the same filter can use an index. Either way the whole filter is checked on every candidate, so an index only skips work and never changes the result.