
18/11/2024
MongoDB is known for its schema-less design and rich features, one of which is indexing. Indexes are crucial in database systems as they improve the speed of data retrieval operations. Here's a breakdown of the different types of indexes supported by MongoDB:
Single field indexes are the simplest form of indexes and are created on a single field of a document. By default, MongoDB creates an index on the _id field of every collection. This type of index helps speed up queries that filter based on a specific field.
db.collection.createIndex({ fieldName: 1 }) // 1 for ascending, -1 for descending
These indexes are formed by combining multiple fields into a single index. Compound indexes optimize queries that filter or sort using multiple fields. The order of the fields in a compound index is significant as it affects the query performance.
db.collection.createIndex({ field1: 1, field2: -1 })
Multikey indexes enable indexing of fields that hold arrays within documents. When you create a multikey index on an array field, MongoDB creates an index entry for each element in the array, allowing efficient querying of array values.
db.collection.createIndex({ arrayField: 1 })
Used for querying spatial data, geospatial indexes enhance queries that involve geographical coordinates. MongoDB supports two types of geospatial indexes: 2D indexes for flat geometries and 2DSphere indexes for spherical geometries (i.e., Earth).
db.collection.createIndex({ location: "2dsphere" })
Text indexes allow for text search queries on string content. MongoDB’s text search can identify words and phrases in documents, enabling full-text search capabilities. It can be created on one or multiple string fields.
db.collection.createIndex({ fieldName: "text" })
Hashed indexes are used primarily for sharding, which is MongoDB’s method of distributing data across multiple servers. These indexes use a hash of the indexed field's value, ensuring an even distribution of data across shards.
db.collection.createIndex({ fieldName: "hashed" })
Partial indexes only include documents that meet a specified filter, making them efficient for queries often run against a subset of data. This type of index helps save space and speeds up certain queries.
db.collection.createIndex({ fieldName: 1 }, { partialFilterExpression: { status: "active" } })
Wildcard indexes create an index on all fields of documents or on specific fields matching a certain pattern. They are especially useful for unstructured data or when fields in documents are not known in advance.
db.collection.createIndex({ "$**": 1 })
MongoDB provides a flexible approach to indexing, allowing developers to tailor indexes to their application needs. Each type offers unique advantages for speeding up data retrieval and improving overall application performance, making it essential to choose the right type based on your use case.
18/11/2024 | MongoDB
18/11/2024 | MongoDB
18/11/2024 | MongoDB
18/11/2024 | MongoDB
18/11/2024 | MongoDB
18/11/2024 | MongoDB
18/11/2024 | MongoDB