Navigation
This version of the manual is no longer supported.

Optimization Strategies for MongoDB Applications

On this page

Overview

There are many factors that can affect performance of operations in MongoDB, including index use, query structure, data modeling, application design and architecture, as well as operational factors including architecture and system configuration. This document addresses key application optimization strategies, and includes examples and links to relevant reference material.

Strategies

This section describes techniques for optimizing database performance with MongoDB with particular attention to query performance and basic client operations.

Use Indexes

For commonly issued queries, create indexes. If a query searches multiple fields, create a compound index. Scanning an index is much faster than scanning a collection. The indexes structures are smaller than the documents reference, and store references in order.

Example

If you have a posts collection containing blog posts, and if you regularly issue a query that sorts on the author_name field, then you can optimize the query by creating an index on the author_name field:

db.posts.ensureIndex( { author_name : 1 } )

Indexes also improve efficiency on queries that routinely sort on a given field.

Example

If you regularly issue a query that sorts on the timestamp field, then you can optimize the query by creating an index on the timestamp field:

Creating this index:

db.posts.ensureIndex( { timestamp : 1 } )

Optimizes this query:

db.posts.find().sort( { timestamp : -1 } )

Because MongoDB can read indexes in both ascending and descending order, the direction of a single-key index does not matter.

Indexes support queries, update operations, and some phases of the aggregation pipeline.

Index keys that are of the BinData type are more efficiently stored in the index if:

  • the binary subtype value is in the range of 0-7 or 128-135, and
  • the length of the byte array is: 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, or 32.

Limit Results

MongoDB cursors return results in groups of multiple documents. If you know the number of results you want, you can reduce the demand on network resources by issuing the cursor.limit() method.

This is typically used in conjunction with sort operations. For example, if you need only 10 results from your query to the posts collection, you would issue the following command:

db.posts.find().sort( { timestamp : -1 } ).limit(10)

For more information on limiting results, see cursor.limit()

Use Projections to Return Only Necessary Data

When you need only a subset of fields from documents, you can achieve better performance by returning only the fields you need:

For example, if in your query to the posts collection, you need only the timestamp, title, author, and abstract fields, you would issue the following command:

db.posts.find( {}, { timestamp : 1 , title : 1 , author : 1 , abstract : 1} ).sort( { timestamp : -1 } )

For more information on using projections, see Result Projections.

Use the Database Profiler to Evaluate Performance

MongoDB provides a database profiler that shows performance characteristics of each operation against the database. Use the profiler to locate any queries or write operations that are running slow. You can use this information, for example, to determine what indexes to create.

For more information, see Database Profiling.

Use db.currentOp() to Evaluate Performance

The db.currentOp() method reports on current operations running on a mongod instance. For documentation of the output of db.currentOp() see Current Operation Reporting.

Use $explain to Evaluate Performance

The explain() method returns statistics on a query, and reports the index MongoDB selected to fulfill the query, as well as information about the internal operation of the query.

Example

To use explain() on a query for documents matching the expression { a: 1 }, in the collection records, use an operation that resembles the following in the mongo shell:

db.records.find( { a: 1 } ).explain()

Use $hint to Select a Particular Index

In most cases the query optimizer selects the optimal index for a specific operation; however, you can force MongoDB to use a specific index using the hint() method. Use hint() to support performance testing, or on some queries where you must select a field or field included in several indexes.

Use the Increment Operator to Perform Operations Server-Side

Use MongoDB’s $inc operator to increment or decrement values in documents. The operator increments the value of the field on the server side, as an alternative to selecting a document, making simple modifications in the client and then writing the entire document to the server. The $inc operator can also help avoid race conditions, which would result when two application instances queried for a document, manually incremented a field, and saved the entire document back at the same time.

Perform Server-Side Code Execution

For some kinds of operations, you can perform operations on the mongod server itself rather than writing a client application to perform a simple task. This can eliminate network overhead for client operations for some basic administrative operations. Consider the following example:

Example

For example, if you want to remove a field from all documents in a collection, performing the operation directly on the server is more efficient than transmitting the collection to your client and back again.

For more information, see the Server-side JavaScript page.

Use Capped Collections

Capped Collections are circular, fixed-size collections that keep documents well-ordered, even without the use of an index. This means that capped collections can receive very high-speed writes and sequential reads.

These collections are particularly useful for keeping log files but are not limited to that purpose. Use capped collections where appropriate.

Use Natural Order

To return documents in the order they exist on disk, return sorted operations using the $natural operator. Natural order does not use indexes but can be fast for operations when you want to select the first or last items on disk. This is particularly useful for capped collections.

See also

sort() and limit().