O'Reilly logo

MongoDB: The Definitive Guide, 2nd Edition by Kristina Chodorow

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 5. Indexing

This chapter introduces MongoDB’s indexing, which allows you to optimize your queries and is even required for certain types of queries:

  • What indexing is and why you’d want to use it

  • How to choose which fields to index

  • How to enforce and evaluate index usage

  • Administrative details on creating and removing indexes

Choosing the right indexes for your collections is critical to performance.

Introduction to Indexing

A database index is similar to a book’s index. Instead of looking through the whole book, the database takes a shortcut and just looks at an ordered list that points to the content, which allows it to query orders of magnitude faster.

A query that does not use an index is called a table scan (a term inherited from relational databases), which means that the server has to “look through the whole book” to find a query’s results. This process is basically what you’d do if you were looking for information in a book without an index: you start at page 1 and read through the whole thing. In general, you want to avoid making the server do table scans because it is very slow for large collections.

For example, let’s create a collection with 1 million documents in it (or 10 million or 100 million, if you have the patience):

> for (i=0; i<1000000; i++) {
...     db.users.insert(
...         {
...              "i" : i, 
...              "username" : "user"+i,
...              "age" : Math.floor(Math.random()*120), 
...              "created" : new Date()
...         }
...     );
... }

If we do a query on this collection, we can use the explain() function ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required