v0.7.0
Menu

#Indexes

Without an index, a filter on a field looks at every document of the collection. An index maps a field's value to the documents that have it, so equality, $in and range lookups skip most of them. The full filter is always checked again on the documents the index finds, so an index only skips work and never changes a result.

All indexes live in memory like the data. Each costs RAM: about 66 bytes a document for a compound index, and more for a unique one, and a single-field index on a number or string stores each id twice (once for equality, once for ranges).

#createIndex

On one field or path:

{"cmd":"createIndex","collection":"users","field":"address.city"}

On several, a compound index, with fields (two to 32 paths):

{"cmd":"createIndex","collection":"users","fields":["address.city","age"]}

Add "unique":true for a unique index:

{"cmd":"createIndex","collection":"users","fields":["address.city","age"],"unique":true}

The reply is {"created":true,"ok":true}. Asking for an index that already exists, with the same fields and the same unique, replies "created":false. The same fields with another unique is an invalid error. Indexes are logged, so a restart rebuilds them. It needs the readWrite role, and is refused inside a transaction.

#dropIndex and listIndexes

{"cmd":"dropIndex","collection":"users","fields":["address.city","age"]}
{"dropped":true,"ok":true}

dropIndex takes field or fields, the same as createIndex. listIndexes needs only read:

{"cmd":"listIndexes","collection":"users"}
{"indexes":[{"fields":["address.city"],"unique":false},{"fields":["address.city","age"],"unique":true}],"ok":true}

#What an index answers

Filter on an indexed field Uses the index
equality: {"age":25} yes
$in yes (not with a $regex item)
$gt, $gte, $lt, $lte on numbers or strings yes
$ne, $nin, $not, $exists, $mod, $type, $regex, $or no, it is a scan
a range on bools, ids, null, documents or arrays no, it is a scan
equality on an array or document value no, it is a scan
  • A filter's top-level entries are ANDed, so any one entry the index can answer gives a list that holds every match. Only one index is used per query, the first usable one, not the most selective.
  • _id in a filter uses the document map directly and needs no index.
  • 1 and 1.0 are one key. Ranges only match the same type, so a number range never returns a string.
  • An index on a field that holds arrays also holds each element, so {"tags":"red"} uses an index on tags. The same goes for a path that goes through an array: an index on items.price holds every item's price.
  • A path index follows $set and $inc on that path. A field that is missing counts as null in the index.
  • Dates can be indexed, and found by equality, $in and sort. A range over dates is a scan.
  • A stored regex is not indexed.

#Compound indexes

A compound index serves equality on its leading fields and then a range on the next one. For ["kind","created"], these use it:

{"cmd":"find","collection":"events","filter":{"kind":"login","created":{"$gte":{"$date":1700000000000},"$lt":{"$date":1700086400000}}}}

The planner picks a compound index only when it fixes at least two conditions. Not served: $in, $or, equality on an array or document value, and a sort on only some of the remaining fields. A compound index cannot hold arrays in two of its fields in one document (cannot index parallel arrays). A missing field counts as null.

#Unique indexes

A unique index refuses an insert, update or upsert that would give a second document the same key, with the code duplicate_key. A missing field counts as null, so only one document can lack it, as in MongoDB. Uniqueness is across documents, not inside one.

  • Creating one over data that already has duplicates fails, and creates nothing.
  • An insert is checked before anything changes. An update applies document by document and undoes the earlier ones when it is refused.
  • A log replay never enforces uniqueness, so a log cannot fail to open because of it.

$merge with an on other than _id needs a unique index on exactly those fields.

#Sorting through an index

A find with a sort and a limit on one indexed field reads the documents off the index in order and stops after skip + limit matches. It is used only when no other index can pick out the candidates, because those are usually few and sorting them is cheaper.

{"cmd":"find","collection":"users","filter":{},"sort":{"age":1},"limit":10}
  • Sorting by more than one field, or without a limit, does not use a single-field index.
  • A compound index is walked for a sort only when the sort fields are exactly the fields after the equality prefix, in order, all in one direction, and no array or document value is in the index.
  • A field that has an array in any document cannot be walked in order, so the server finds the matches and sorts them, as for a field with no index. Equality and range lookups are not affected.
  • Worst case, a filter that matches almost nothing on a field with no other index: the walk cannot stop early and costs about as much as the scan it replaces.

#Not supported

  • Indexes inside a transaction (createIndex and dropIndex are refused there).
  • Partial, sparse, text, geospatial and TTL indexes.
  • Choosing an index with a hint, or a way to see the plan the server chose.