Learning MongoDB

0% completed

Previous
Next
Single Field Indexes

Single field indexes in MongoDB are the simplest type of index and are used to index a single field within a document. They can significantly enhance the performance of read operations by allowing MongoDB to quickly locate documents that match a query condition on that indexed field. Single field indexes are particularly useful for queries that filter or sort on a single field.

Syntax

db.collection.createIndex({ <field>: <type> }, { <option1>: <value1>, <option2>: <value2>, ... })
  • field: The field to be indexed.
  • type: The type of index, usually 1 for ascending order or -1 for descending order.
  • option1, option2: Optional parameters to customize the index behavior (e.g., unique, background).

Example Setup

Let's insert some documents into the employees collection:

db.employees.insertMany([ { name: "John Doe", age: 29, department: "HR" }, { name: "Jane Smith", age: 34, department: "Finance" }, { name: "Emily Davis", age: 42, department: "IT" }, { name: "Michael Brown", age: 36, department: "Marketing" }, { name: "Sarah Wilson", age: 30, department: "Sales" } ])

Index Creation Example

Create an index on the name field:

db.employees.createIndex({ name: 1 }, { unique: true, name: "uniqueNameIndex" })

Explanation:

  • db.employees.createIndex({ name: 1 }, { unique: true, name: "uniqueNameIndex" }): Creates a unique index on the name field in ascending order. This index ensures that each name in the employees collection is unique.

Querying Using the Indexes

Example 1: Query by Indexed Field

db.employees.find({ name: "Jane Smith" })

Explanation:

  • This query uses the index on the name field to quickly locate the document where name is "Jane Smith".

Example 2: Sort by Indexed Field

db.employees.find().sort({ name: 1 })

Explanation:

  • This query uses the index on the name field to efficiently sort the documents by name in ascending order.

Use Cases or Benefits

  1. Enhanced Query Performance: Single field indexes dramatically improve the performance of queries that filter by the indexed field.
  2. Efficient Sorting: Indexes facilitate quick sorting of query results, which is essential for large datasets.
  3. Uniqueness Enforcement: Unique indexes ensure that the indexed field values are unique across all documents, preventing duplicate entries.

Considerations

  1. Index Overhead: Indexes consume additional disk space and can increase the overhead on write operations, as the index needs to be updated whenever a document is inserted, updated, or deleted.
  2. Index Selection: Not all fields benefit equally from indexing. Frequently queried fields are ideal candidates for indexing, while fields with low query frequency may not benefit much.
  3. Write Performance: Each index adds overhead to write operations (inserts, updates, deletes), so balance is needed between read and write performance.

Single field indexes in MongoDB are essential tools for optimizing query performance, supporting efficient sorting, and enforcing uniqueness. By understanding the syntax, benefits, and considerations, you can effectively use single field indexes to improve the efficiency and performance of your MongoDB queries.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next