JIYIK CN >

Current Location:Home > Learning > DATABASE > MongoDB >

Creating Indexes in MongoDB

Author:JIYIK Last Updated:2025/04/28 Views:

Indexes help resolve queries efficiently. Without indexes, MongoDB must iterate through every document in the collection to find the documents that match the query.

It will waste time and require MongoDB to handle such information. Therefore, in today’s article, we will learn how to create indexes in MongoDB.


Creating Indexes in MongoDB

An index is a specialized data structure that holds a limited subset of a dataset in an accessible format. The values ​​of a specific field or set of fields are stored in the index, sorted by the field values ​​indicated in the index.

MongoDB supports different types of indexes. Single field, composite index, multi-key index, geospatial index, hash index, and clustered index are some of them.

grammar:

db.COLLECTION.createIndex({ KEY_NAME:1 })
  1. KEY_NAME is the name of the field to be indexed.
  2. The previous point indicates that the order should be ascending. So the index must be constructed with -1 in descending order.
  3. The createIndex() method accepts a list of optional parameters. The list is as follows.
    • unique is a boolean variable that creates a unique index, preventing the collection from accepting insertion of documents where the index key or keys already exist. To create a unique index, enter true. The default value is set to false.
    • Boolean variable background Builds the index in the background so it does not interfere with other database operations. To build in the background, specify true. The default value is set to false.
    • If the boolean variable sparse is set to true, the index will only reference documents with the specified fields. Although these indexes take up less space, they may behave differently (especially with sorting). false is the default setting.
    • The string variable name contains the name of the index. If no index name is provided, MongoDB creates one by concatenating the name of the index field and the sort order.
    • The default language for string variables is the language used to build the stop word list and the stemmer and tokenizer standards for the text index.
    • The integer variable expireAfterSeconds defines a time-to-live (TTL) value in seconds that limits how long MongoDB retains documents in this collection.

Let's use the following example to understand the above ideas:

> db.users.createIndex({ "email": 1, "country": -1 })

In the example above, we created a new composite index where email is stored in descending order and country is sorted in ascending order. Every time a query is executed, the index is first sorted by the user's email and then by the country within each email value.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Date comparison in MongoDB

Publish Date:2025/04/28 Views:169 Category:MongoDB

This MongoDB tutorial discusses the issue of returning Date-based queries. In addition, there is also a good tutorial on how to use Date Range queries in MongoDB. Using date ranges in MongoDB We will learn to write MongoDB date range querie

Find objects between two dates in MongoDB

Publish Date:2025/04/28 Views:94 Category:MongoDB

In this article, the problem of finding objects between two dates was briefly discussed. In addition, the operators $gte, $lte, $gt, and $lt used for this purpose were briefly explained in detail. Querying date ranges in MongoDB This sectio

Comparing Dates in MongoDB

Publish Date:2025/04/28 Views:115 Category:MongoDB

Date is a common field in most databases, and sometimes we need to find exact documents from a collection in MongoDB. For example, if we have a collection of orders, we might search for those documents before or after a specific date. In th

Convert string to date in MongoDB

Publish Date:2025/04/28 Views:162 Category:MongoDB

MongoDB is an excellent platform that is growing in popularity. Among the various features it offers, MongoDB also allows you to convert data from one type to another. This may seem like a complex function, but it is very simple to execute.

Building the MongoDB REST API

Publish Date:2025/04/28 Views:70 Category:MongoDB

MongoDB is a flexible and scalable document-oriented database system that is widely used for large-volume data storage. It uses documents and collections instead of the traditional rational database approach of using tables and rows. MongoD

Using ORM with MongoDB

Publish Date:2025/04/28 Views:117 Category:MongoDB

MongoDB introduces a NoSQL solution for data storage and management, consisting of documents represented in JSON style. Like other database systems, MongoDB can also use ORM. In this article, we will explain the concepts of ORM and general

Locking mechanism in MongoDB

Publish Date:2025/04/28 Views:95 Category:MongoDB

In database management systems, locking mechanisms ensure the consistency of the entire result. For example, if some writing process is in progress on the data, a read command cannot be executed at the same time. Database resources are lock

Unique Index in MongoDB

Publish Date:2025/04/28 Views:130 Category:MongoDB

In this article, you'll learn about unique indexes, including what they are and how to create them in MongoDB. Additionally, the process of making a user's email unique in MongoDB is briefly described. The contents of this article are as fo

Composite Indexes in MongoDB

Publish Date:2025/04/28 Views:151 Category:MongoDB

Sometimes we need to create an index that contains multiple fields. For example, if your document contains a field called Sex, it may contain two other fields, such as Male and Female. These fields may have values ​​like Yes or No. In t

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial