0% completed
Indexes in MongoDB are not one-size-fits-all; there are several types of indexes that cater to different querying needs.
Understanding the various index types helps in designing efficient and optimized database schemas. In this lesson, we will explore different types of indexes, including their syntax, parameters, and examples.
First, let's insert some documents into the library
collection to work with:
db.library.insertMany([ { title: "To Kill a Mockingbird", author: "Harper Lee", genre: "Fiction", pages: 281 }, { title: "1984", author: "George Orwell", genre: "Dystopian", pages: 328 }, { title: "Moby Dick", author: "Herman Melville", genre: "Adventure", pages: 635 }, { title: "The Great Gatsby", author: "F. Scott Fitzgerald", genre: "Fiction", pages: 180 }, { title: "War and Peace", author: "Leo Tolstoy", genre: "Historical", pages: 1225 } ])
A single field index is used to index a single field of a document. It can speed up queries that filter or sort by that field.
db.collection.createIndex({ <field>: <type> })
db.library.createIndex({ author: 1 })
db.library.createIndex({ author: 1 })
: This command creates an index on the author
field in ascending order. This index will speed up queries that filter or sort by the author
field.A compound index is used to index multiple fields within a document. It can speed up queries that filter or sort by multiple fields.
db.collection.createIndex({ <field1>: <type1>, <field2>: <type2> })
db.library.createIndex({ genre: 1, pages: -1 })
db.library.createIndex({ genre: 1, pages: -1 })
: This command creates a compound index on the genre
field in ascending order and the pages
field in descending order. This index will speed up queries that filter or sort by both genre
and pages
.A multikey index is used to index an array field in a document. It indexes each element of the array, allowing queries to filter or sort based on array values.
db.collection.createIndex({ <arrayField>: <type> })
Insert documents with an array field into the library
collection:
db.library.insertMany([ { title: "In Search of Lost Time", authors: ["Marcel Proust"], genre: "Modernist" }, { title: "Ulysses", authors: ["James Joyce"], genre: "Modernist" }, { title: "Don Quixote", authors: ["Miguel de Cervantes"], genre: "Adventure" } ])
Command:
db.library.createIndex({ authors: 1 })
db.library.createIndex({ authors: 1 })
: This command creates a multikey index on the authors
array field. This index will speed up queries that filter or sort by the elements of the authors
array.A text index is used to perform text searches on a field or fields. It supports language-specific text search, allowing for efficient search of words within string content.
db.collection.createIndex({ <field>: "text" })
Command:
db.library.createIndex({ title: "text", genre: "text" })
db.library.createIndex({ title: "text", genre: "text" })
: This command creates a text index on the title
and genre
fields. This index enables text search queries on these fields.A geospatial index is used to support queries that involve geographic locations. This type of index is essential for applications that require location-based queries, such as finding nearby points of interest or performing spatial analysis.
db.collection.createIndex({ <locationField>: "2dsphere" })
Setup:
Insert documents with location data into the places
collection:
db.places.insertMany([ { name: "Central Park", location: { type: "Point", coordinates: [-73.968285, 40.785091] } }, { name: "Golden Gate Bridge", location: { type: "Point", coordinates: [-122.478255, 37.819929] } } ])
Command:
db.places.createIndex({ location: "2dsphere" })
db.places.createIndex({ location: "2dsphere" })
: This command creates a geospatial index on the location
field using the "2dsphere" type. This index will speed up queries that involve geographic location data.A hashed index is used to support hash-based sharding and speeds up equality queries. This type of index is particularly useful for distributing data evenly across a sharded cluster and improving query performance for equality comparisons.
db.collection.createIndex({ <field>: "hashed" })
db.library.createIndex({ title: "hashed" })
db.library.createIndex({ title: "hashed" })
: This command creates a hashed index on the title
field. This index supports hash-based sharding and speeds up equality queries on the title
field.A wildcard index is used to index fields that have a dynamic schema. This type of index is useful when the document structure is not consistent, and you want to index all fields or specific fields that match a pattern.
Command:
db.library.createIndex({ "$**": 1 })
db.library.createIndex({ "$**": 1 })
: This command creates a wildcard index that indexes all fields in the documents. This index is beneficial for collections with a dynamic schema where the document structure can vary.Understanding the different types of indexes in MongoDB is crucial for optimizing query performance and ensuring efficient data retrieval. Each index type serves a specific purpose and is suited for different query patterns. By using the appropriate index type, you can significantly enhance the performance of your MongoDB queries.
In next lessons, we will explore more specific indexing strategies, such as single field indexes, compound indexes, multikey indexes, and text indexes.
.....
.....
.....