Introduction to MongoDB collection.createIndex() Method

MongoDB is a popular NoSQL database used for storing and managing semi-structured data. To improve the performance and query speed of the database, MongoDB provides various types of indexes, including single-key indexes, compound indexes, and geospatial indexes, etc. The method for creating indexes is to use the createIndex() method.

If you want to create multiple indexes at the same time, use the createIndexes() method.

Syntax

The basic syntax of the createIndex() method is as follows:

db.collection.createIndex(keys, options)

Here, collection is the name of the collection on which to create an index; keys are the field names and sorting methods to create an index, which can be an object or a string with multiple fields separated by commas; options is an optional option object used to specify the type, name, uniqueness, etc. of the index.

Usage

Indexes in MongoDB can speed up query operations, which is particularly important for large data collections. Usually, the createIndex() method can be used in the following scenarios:

  • For fields that are frequently queried, single-key indexes or compound indexes can be created to speed up query speed;
  • For queries that need to return results in a specific order, sorting indexes can be created;
  • For fields that need to perform full-text searches, text indexes can be created;
  • For geographic location fields that need to perform geospatial queries, geospatial indexes can be created.

Examples

Now let’s look at two examples of creating indexes using the createIndex() method.

Example 1: Creating a Single-Key Index

Assume that we have a collection named users that contains the following documents:

{ "_id" : ObjectId("61817a49d414fb6ca06a6c38"), "name" : "Alice", "age" : 25, "gender" : "female" }
{ "_id" : ObjectId("61817a49d414fb6ca06a6c39"), "name" : "Bob", "age" : 30, "gender" : "male" }
{ "_id" : ObjectId("61817a49d414fb6ca06a6c3a"), "name" : "Charlie", "age" : 35, "gender" : "male" }

Now we want to create a single-key index on the age field, which can be done using the following code:

db.users.createIndex({ age: 1 })

This code will create an ascending single-key index on the users collection for speeding up queries by age.

Example 2: Creating a Compound Index

Assuming we have a collection named sales, which contains the following documents:

{ "_id" : ObjectId("61817a49d414fb6ca06a6c3b"), "date" : ISODate("2021-11-01T00:00:00Z"), "product" : "A", "price" : 10, "quantity" : 100 }
{ "_id" : ObjectId("61817a49d414fb6ca06a6c3c"), "date" : ISODate("2021-11-01T00:00:00Z"), "product" : "B", "price" : 15, "quantity" : 200 }
{ "_id" : ObjectId("61817a49d414fb6ca06a6c3d"), "date" : ISODate("2021-11-02T00:00:00Z"), "product" : "A", "price" : 12, "quantity" : 150 }
{ "_id" : ObjectId("61817a49d414fb6ca06a6c3e"), "date" : ISODate("2021-11-02T00:00:00Z"), "product" : "B", "price" : 18, "quantity" : 250 }

Now, we want to create a compound index on the date and product fields to accelerate queries by date and product. We can use the following code:

db.sales.createIndex({ date: 1, product: 1 })

This code creates an ascending compound index on the sales collection to speed up queries by date and product.

Conclusion

createIndex() method is one of the primary ways to create indexes in MongoDB. By creating appropriate indexes, query speed and performance can be improved, making the database more efficient and manageable. In practical use, suitable index types and creation methods should be selected according to the characteristics of the data collection and the query requirements.