Use Indexes for blazing-fast queries on your API for MongoDB Data
What Are Indexes?
Indexes are specialized data structures that make querying your data roughly an order of magnitude faster. Indexes store a subset of your collection’s data in memory. This subset consists of the values of the fields you’ve designated for indexing and pointers to their corresponding documents in your collection. Indexes are also utilized to store these field values in sorted order to ensure that sort performance scales as your data grows.
How Indexes Improve Performance and Cost
Indexes are stored in memory, are kept in sorted order, and prevent queries from having to scan every document in a collection when querying an indexed field. These three characteristics all contribute to why querying indexed data is roughly an order of magnitude faster!
Better yet, you will also pay roughly 10x less for each query on indexed data because the API for MongoDB charges based on your compute and storage usage, known as Request Units (RUs).
Indexes are updated when database writes occur, so writes cause additional load as more indexes are added. However, the performance and cost-benefit of using indexes far outweigh the additional write costs in nearly all use cases. Indexes can be managed using all the MongoDB tools you know and love as well as the index policy editor in the Azure Portal.
Let’s dive into the three things that are most important for MongoDB developers to know about regarding indexing when building their applications on the API for MongoDB.
Single Field Indexes
Single field indexes index one field to make querying that field fast and cheap. Unlike in native MongoDB, in the API for MongoDB, single field indexes can be used by your query in any combination to filter on multiple fields. No compound indexes are required to filter on multiple fields, just single field indexes on each field. To boost performance and cost savings, we recommend creating a single field index for every field that you plan to filter on in your queries.
Compound indexes provide two benefits:
- Ensure that filtering on multiple fields remains performant and consistent as your data scales. A compound index must be created for every combination of fields that you’d like to sort on. Compound indexes are not required for multiple field filtering or single field sorting, but they are required if you want to perform sort operations on multiple fields.
- Fields within a compound index can be used in any combination for filtering – even if you omit some of the fields.
The main takeaway: Create a compound index if you need to sort on multiple fields.
If you have an API for MongoDB account, you may already be using wildcard indexes since they are enabled by default on new collections. Unlike in native MongoDB, wildcard indexes are utilized for filtering on any combination of fields. Wildcard indexes are a great way to get started with querying in the API for MongoDB. We recommend that you replace your wildcard indexes with single field indexes over time to optimize write performance and costs.
Indexes are powerful features that make querying your data fast and cheap. The API for MongoDB benefits from being built on Azure Cosmos DB by offering more flexible indexing compared to native MongoDB. Single field indexes are utilized for any combination of filtering, and compound indexes ensure multi-field sorts offer consistent performance at scale as well as flexible filtering on any combination of fields. To learn more and see examples, visit our API for MongoDB indexing documentation.
Get started now!
Azure Cosmos DB is a fully managed NoSQL database service for modern app development. You can try Azure Cosmos DB API for MongoDB free for 30 days – no subscription or credit-card number required.
Ready to jump in? Create an Azure Cosmos DB account and activate the free tier to get the first 1,000 RU/s and 25 GB of storage for free every month.