Single-field indexes in MongoDB improve query performance by indexing one specific field, allowing faster lookups and sorting on that field.
- Indexes store field values in an ordered structure for quick retrieval.
- A single-field index is created on one field of a document.
- Speeds up queries and sorting based on that field.
- Reduces the need for full collection scans.
Index Properties
MongoDB allows specifying additional options when creating indexes to customize their behavior. Some common options include:
- Unique Index: Ensures that indexed field values are unique across the collection.
- Sparse Index: Indexes only documents that contain the indexed field, ignoring documents that do not have the field.
- Partial Index: Indexes documents based on a specified filter expression.
Examples of Single Field Indexes
Here we will consider a collection called books which contains information in various documents are shown below.
(
{
"title": "MongoDB Basics",
"author": "John Doe",
"publishedYear": 2021
},
{
"title": "Advanced MongoDB Techniques",
"author": "Jane Smith",
"publishedYear": 2020
},
{
"title": "Mastering MongoDB",
"author": "Alice Johnson",
"publishedYear": 2019
}
)
Example 1: Create an Index on a Single Field
To create a single field index in MongoDB, you can use the createIndex() method.
db.books.createIndex({ title: 1 })Output:
title_1This output confirms that the index was successfully created and added to the collection.
Example 2: Create a Descending Index on a Single Field
Create an index to sort and retrieve books by their publication year in descending order.
db.books.createIndex({ publishedYear: -1 })Output:
publishedYear_-1Example 3: Create an Index on an Embedded Field
Modify the documents to include embedded fields.
[
{
"title": "MongoDB Basics",
"author": { "firstName": "John", "lastName": "Doe" },
"publishedYear": 2021
},
{
"title": "Advanced MongoDB Techniques",
"author": { "firstName": "Jane", "lastName": "Smith" },
"publishedYear": 2020
},
{
"title": "Mastering MongoDB",
"author": { "firstName": "Alice", "lastName": "Johnson" },
"publishedYear": 2019
}
]
Query:
Create an index to quickly search for books by the author's first name in the books collection.
db.books.createIndex({ "author.firstName": 1 })Output:
author.firstName_1Example 4: Create an Index on an Embedded Document
Create an index to efficiently search for books based on the entire author object in the books collection.
db.books.createIndex({ author: 1 })Output:
author_1Benefits of Single Field Indexes
Single-field indexes offer several benefits, as defined below:
- Improved Query Performance: Speeds up queries by allowing fast lookups on the indexed field.
- Reduced Disk I/O: Lowers disk reads by avoiding full collection scans in the database.
- Optimized Sorting: Improves sorting speed when results are ordered by the indexed field.
- Efficient Data Access: Enables MongoDB to quickly locate and fetch matching documents.
Considerations for Single-Field Indexes
While single field indexes provide significant performance benefits, it's essential to consider the following factors:
- Index Maintenance Overhead: Indexes use extra storage and slow down writes due to update overhead.
- Index Selectivity: Index fields with high uniqueness low-selectivity fields give little benefit.
- Query Patterns: Create indexes on fields frequently used in filters, sorting or aggregation.