liveBook · Manning

Single-column index

As explained in MongoDB 8.0 in Action, Third Edition

A single-field index is an index whose key consists of one document field and whose entries are ordered by that field's values.

A single-column index, called a single-field index in MongoDB, stores values from one document field in sorted order. It is useful when an application frequently queries that field. The field can be at the top level or inside an embedded document. section 7.2.1

db.movies.createIndex({ "runtime": 1 })

Create an ascending index on movies.runtime with createIndex(). The value 1 specifies ascending order; -1 specifies descending order. section 7.2.1

Check that the index helps the intended query:

  • Run db.movies.getIndexes() to confirm that runtime_1 exists alongside the automatically created _id index. section 7.2.1
  • Run db.movies.find({ runtime: 100 }).explain("executionStats"). An IXSCAN on runtime_1 followed by FETCH shows the index locating matches before MongoDB retrieves their documents. section 7.2.1
  • Inspect plans rather than assuming every predicate benefits equally: single-field indexes may offer limited benefit for $ne, $nin, and $not. section 7.6.5 section 7.8

A single-field index can also have a time-to-live (TTL) role. For a date field, createIndex({ date: 1 }, { expireAfterSeconds: 31536000 }) makes documents expire one year after the indexed date. Use 0 when the field contains each document’s intended expiration time. section 7.4.3

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Walks through index creation and execution-plan checks, but does not detail the index’s internal storage structure.
Examples
Uses a movies.runtime index with getIndexes() and explain("executionStats"), plus a date-based transactions TTL index.
What it assumes
Assumes MongoDB collections and field-based queries while introducing createIndex() syntax and index-verification commands.
What you can do after
Apply it via mongosh createIndex() and diagnose its use with getIndexes() and explain("executionStats").
How it connects
Connects single-field indexes to query planners, execution plans, sorting, and time-to-live indexes.

Also explained in

Compare how 2 books explain single-column index →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.