Topics such as replication and sharding are beyond the scope of this book. However, there are a number of things developers can do to optimize the performance of your MongoDB database.
Mainly, we'll talk about cover indexing and tuning queries for performance.
In many database systems, adding an index
in a field when appropriate can speed up querying. Queries are optimized when performed on indexed fields. MongoDB is no different.
The downside to indexes is that they add some extra time to write operations. They also take up extra space in the database. It makes sense to index wisely. When considering adding indexes, you want to think about whether you expect more read than write operations. This would be a plus for adding additional indexes.
Let's add an index to our user collection. We'll say that we want to frequently look up our users by their last names. It makes sense to add an index
on the lastname
field, as shown in the following commands:
> db...