Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversHalloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk4 min

MongoDB Indexes and B-Trees: How They Work and How to Choose a Compound Index

MongoDB indexes use ordered B-tree structures to support eligible reads. Learn how compound field order and sort direction shape index use—and how to verify an index’s costs and benefits.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

MongoDB indexes are ordered lookup structures that can help eligible queries find documents without scanning an entire collection. They use B-tree structures, and with a compound index the order of its fields determines which query and sort patterns it can support. The right index depends on the queries your application actually runs—and it comes with storage and write-maintenance costs.

How MongoDB indexes and B-trees work

An index is a separate, ordered structure associated with a collection. It stores values for one field or a set of fields, along with information MongoDB uses to find the corresponding documents. For a query that can use the index, MongoDB can search the ordered values rather than examine every document. An index does not make every query faster: whether it helps depends on the query, the data, and the available index.

As an Amazon Associate I earn from qualifying purchases.

MongoDB documents its indexes as B-tree data structures. In a B-tree, values are kept in sorted order so the database can navigate to matching values and, where the query permits, scan a relevant range. MongoDB’s index overview and index types documentation describe the structure and the different kinds of indexes available.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

MongoDB supports several index types, including single-field, compound, multikey, wildcard, geospatial, hashed, text, and clustered indexes. These are not interchangeable choices: specialized types address particular data shapes or query operations, while a compound index orders values from multiple fields to support particular combinations of filters and sorts.

How to choose the order of a compound index

A compound index stores values for multiple fields in the order specified by its key pattern. That order controls which leading prefixes of the index are available to a query. As MongoDB puts it, “The B-tree created by a compound index stores the sorted data in the order that the index specifies the fields.” See the compound index documentation.

Use the leading-field rule

An index on { title: 1, metacritic: -1 } can support queries on title alone and queries that use both title and metacritic. It does not provide the same prefix support for a query on metacritic alone, because that field comes second. A compound index therefore does not automatically replace separate indexes for every field it contains.

Start with the query shapes you need to serve, then ask which fields must lead for those queries to use an index. If your workload frequently filters by one field on its own, that field may need to lead an index pattern. If queries commonly combine fields, consider an order that preserves useful leading prefixes while also matching the required sort.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Check sort direction as well as field order

For a compound sort, MongoDB can traverse an index in the declared direction or in the complete reverse direction. A sort that reverses only one direction in a mixed-direction pattern does not match either traversal. For example, an index on { category: 1, score: -1 } can support a sort on { category: 1, score: -1 } or the complete reverse, { category: -1, score: 1 }. It does not support the mixed pattern { category: 1, score: 1 } by that same direction rule. See MongoDB’s sort results with indexes guidance.

Balance equality, sort, and range needs

For each important query, write down its filter fields, sort pattern, and any range condition. Then assess whether a candidate index has the right leading fields and sort directions for that specific shape. There is no universally best field order: a choice that helps one query may leave another query without a useful leading prefix, and low-selectivity conditions may not reduce enough work to justify an index.

The current MongoDB compound-index manual documents a limit of up to 32 fields in one compound index. This is a technical maximum, not a design target; check the manual for the server version you deploy.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How to verify that an index helps

An index definition alone does not show whether a real query benefits. Inspect the query’s execution plan with explain(), and evaluate it against representative data and workload behavior. MongoDB’s query optimization guidance explains plan inspection and notes that low-selectivity predicates may gain little from an index.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Look at whether the plan uses the intended index and how much work it performs, including documents and index keys examined. A query can be covered when its index contains the fields needed by that query, allowing MongoDB to answer it from the index without fetching collection documents. Coverage is specific to the query and index; it is not an automatic property of an index in general.

What indexes cost

Indexes can reduce read work, but MongoDB must maintain them when documents change and they consume storage. As the MongoDB documentation states, “In write operations, MongoDB must both write the change to the collection and update the index.” Too many indexes can therefore hurt performance, particularly on write-heavy collections. Avoid adding an index for every field that appears in a query; prioritize recurring query patterns and retain indexes that justify their maintenance and storage costs.

WiredTiger compression and deployment scope

WiredTiger is MongoDB’s default storage engine. MongoDB documents prefix compression for indexes as enabled by default, which can reduce index storage. The in-memory representation of an index in WiredTiger’s internal cache differs from its on-disk form, so on-disk size alone does not describe its cache footprint.

These detailed storage-engine behaviors are scoped in MongoDB’s WiredTiger documentation to Atlas Core and self-managed deployments; Atlas Infinite uses a different storage architecture. Compression does not remove the need to account for index memory, storage, and update work when evaluating a design.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.