Composite Indexes in 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 this article, we will discuss about compound indexes, and we will also see an example with explanation to make the topic easier to understand.
MongoDB Indexes
Indexing is very important because it improves query performance and makes it easier to search for specific documents. A composite index is an index that combines multiple fields together.
It is mainly used for fields that depend on the values of other fields. For example, if you have a field called Name, the value of that field may depend on the values of two other fields, FirstName and LastName.
So a composite index is an index on multiple fields. The main difference between a normal index (single index) and a composite index is that a composite index is based on multiple values.
Creating a composite index in MongoDB
MongoDB contains a createIndex()
built-in method called to create any index. To create a compound index, we can follow the following general syntax.
db.collection.createIndex
({
<field1>: <type1>, <field2>: <type2>, …
})
Let's look at the following example to make it clearer.
db.employee.createIndex
({
Name:1, Email:-1
})
In the above example, we created a simple composite index that will sort the employee collection by the field Name in ascending order and then by the field Email in descending order.
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.
Related Articles
$unset operator in MongoDB
Publish Date:2025/04/27 Views:78 Category:MongoDB
-
This article will discuss how the $unset operator works in MongoDB. Additionally, we will demonstrate the use of this operator to remove a field from all documents in a MongoDB collection. $unset operator in MongoDB $unset is an operator us
Compass Filters in MongoDB
Publish Date:2025/04/27 Views:133 Category:MongoDB
-
This short article will cover the various ways to use Compass filters in MongoDB . Compass Filters in MongoDB MongoDB has a GUI called Compass . It is also known as MongoDB GUI. Users can use MongoDB to inspect the contents of their stored
Sorting by timestamp in MongoDB
Publish Date:2025/04/27 Views:54 Category:MongoDB
-
This article will introduce various methods of sorting timestamps in MongoDB. Sorting by timestamp in MongoDB sort() The method will sort documents in MongoDB. The method accepts a document containing a list of fields and the order in which
Deleting a user from a database in MongoDB
Publish Date:2025/04/27 Views:51 Category:MongoDB
-
This article will explain how to delete a user from a MongoDB database. In addition, we will see an example to make the topic easier to understand. Deleting a User from a MongoDB Database Sometimes we need to remove a particular user from t
Deleting items by ID in MongoDB
Publish Date:2025/04/27 Views:159 Category:MongoDB
-
Sometimes we need to delete data from a database based on specified criteria. Unlike other SQL databases, MongoDB does not include SQL queries for this purpose. Instead, it uses commands. This article will discuss how to delete documents ba
Using ISODate for date queries in MongoDB
Publish Date:2025/04/27 Views:77 Category:MongoDB
-
This MongoDB tutorial article will explain Date() the methods. This article introduces different ways to query using dates. Date() Method in MongoDB Date() The method returns the date as a string or a Date object. In the MongoDB shell or mo
Return unique values in MongoDB
Publish Date:2025/04/27 Views:191 Category:MongoDB
-
In this article, we will address how to use the MongoDB distinct() method to return unique values. In addition, returning unique values in arrays and fields is discussed. In MongoDB, sometimes you may want to present or return unique
Querying documents with array size greater than 1 in MongoDB
Publish Date:2025/04/27 Views:154 Category:MongoDB
-
When working with projects where you need to validate the size of an array or find elements whose size is greater or less than a certain length, you might use MongoDB operators such as $size, $where, $exists, etc. The methods discussed belo
Case insensitive query in MongoDB
Publish Date:2025/04/27 Views:168 Category:MongoDB
-
In this article, case insensitive queries are discussed briefly and in detail. Also, case insensitive search queries are explained in detail. This article discusses the following topics. Case-insensitive search Improve case-insensitive regu