Skip to content

MongoDB Advanced Indexing

In MongoDB, advanced indexing refers to index types other than basic indexes. These index types can help us solve some special query requirements.

Basic Concepts

Types of Advanced Indexes

  1. Multikey Indexes: Used to index array fields.
  2. Geospatial Indexes: Used to index geospatial data.
  3. Text Indexes: Used to index text data.
  4. Hashed Indexes: Used to index hash values.

Multikey Indexes

How Multikey Indexes Work

Multikey indexes are a special type of index that can index all elements of array fields. When we create an index on an array field, MongoDB automatically creates a multikey index.

Creating Multikey Indexes

javascript
// Create an index on an array field
db.users.createIndex({ tags: 1 })

// Create an index on a nested array field
db.users.createIndex({ "address.street": 1 })

Querying Multikey Indexes

javascript
// Query documents where the tags field contains "tech"
db.users.find({ tags: "tech" })

// Query documents where the address.street field contains "Main"
db.users.find({ "address.street": /Main/ })

Geospatial Indexes

Types of Geospatial Indexes

  1. 2dsphere Index: Used to index coordinate points on the Earth's surface.
  2. 2d Index: Used to index coordinate points on a plane.

Creating Geospatial Indexes

javascript
// Create a 2dsphere index
db.places.createIndex({ location: "2dsphere" })

// Create a 2d index
db.places.createIndex({ location: "2d" })

Querying Geospatial Data

javascript
// Query documents within a certain distance from a coordinate point
db.places.find({
  location: {
    $near: {
      $geometry: {
        type: "Point",
        coordinates: [-73.98, 40.74]
      },
      $maxDistance: 5000
    }
  }
})

// Query documents within a polygon
db.places.find({
  location: {
    $geoWithin: {
      $geometry: {
        type: "Polygon",
        coordinates: [
          [
            [-73.99, 40.73],
            [-73.98, 40.73],
            [-73.98, 40.74],
            [-73.99, 40.74],
            [-73.99, 40.73]
          ]
        ]
      }
    }
  }
})

Text Indexes

How Text Indexes Work

Text indexes are a special type of index that can index the content of text fields. When we create an index on a text field, MongoDB automatically tokenizes the content of the text field and creates an index.

Creating Text Indexes

javascript
// Create an index on a single text field
db.articles.createIndex({ title: "text" })

// Create an index on multiple text fields
db.articles.createIndex({ title: "text", content: "text" })

// Create an index on all text fields
db.articles.createIndex({ "$**": "text" })

Querying Text Indexes

javascript
// Query documents containing "MongoDB"
db.articles.find({ $text: { $search: "MongoDB" } })

// Query documents containing "MongoDB" or "Node.js"
db.articles.find({ $text: { $search: "MongoDB Node.js" } })

// Query documents containing "MongoDB" but not containing "Node.js"
db.articles.find({ $text: { $search: "MongoDB -Node.js" } })

Hashed Indexes

How Hashed Indexes Work

Hashed indexes are a special type of index that can index hash values. When we create an index on a field, MongoDB automatically calculates the hash value of the field value and creates an index.

Creating Hashed Indexes

javascript
// Create a hashed index on a field
db.users.createIndex({ name: "hashed" })

Querying Hashed Indexes

javascript
// Query documents where the name field equals "John"
db.users.find({ name: "John" })

Best Practices for Advanced Indexing

Choose the Right Index Type

According to the query requirements, we should choose the right index type. For example, if we need to query geospatial data, we should use geospatial indexes; if we need to query text data, we should use text indexes.

Avoid Using Too Many Indexes

Although indexes can improve query performance, using too many indexes can cause performance degradation of insert, update, and delete operations. Therefore, we should only create necessary indexes.

Maintain Indexes Regularly

We should maintain indexes regularly, such as deleting unused indexes or optimizing the structure of indexes.

Summary

In MongoDB, advanced indexing refers to index types other than basic indexes, including multikey indexes, geospatial indexes, text indexes, and hashed indexes. These index types can help us solve some special query requirements. When using advanced indexes, we should choose the right index type according to the query requirements and avoid using too many indexes to ensure the performance of the database. At the same time, we should also maintain indexes regularly to ensure the effectiveness of the indexes.

Content is for learning and research only.