🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this post, we will see how to create, find and drop indexes in MongoDB.
Indexes support the efficient execution of queries in MongoDB. Without indexes, MongoDB must perform a collection scan, i.e. scan every document in a collection, to select those documents that match the query statement. If an appropriate index exists for a query, MongoDB can use the index to limit the number of documents it must inspect.
In this post, you will learn –
- How to Create Indexes: createIndex()
- How to Find Indexes: getIndexes()
- How to Drop Indexes: dropIndex()
1. How to Create Indexes: createIndex()
Creating an Index in MongoDB is done by using the "createIndex()" method.
Syntax:
db.collection_name.createIndex({field_name: 1 or -1})
- The value 1 is for ascending order and -1 is for descending order.
- The db.collection.createIndex() method only creates an index if an index of the same specification does not already exist.
- MongoDB indexes use a B-tree data structure.
Example
Let's say we want to create the index on title field of a post in ascending order:
> db.posts.createIndex({title: 1});
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
Complete example:
> db.posts.createIndex({title: 1});
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.posts.find().pretty();
{
"_id" : ObjectId("5e184a067695f4d696a0598d"),
"title" : "MongoDB Overview",
"description" : "MongoDB is no sql database",
"by" : "Java Guides",
"url" : "https://javaguides.net",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 100
}
{
"_id" : ObjectId("5e184a067695f4d696a0598e"),
"title" : "NoSQL Database",
"description" : "NoSQL database doesn't have tables",
"by" : "Java Guides",
"url" : "https://javaguides.net",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 20,
"comments" : [
{
"user" : "user1",
"message" : "My first comment",
"dateCreated" : ISODate("2013-12-10T09:35:00Z"),
"like" : 0
}
]
}
{
"_id" : ObjectId("5e18544a7695f4d696a0598f"),
"title" : "MongoDB CRUD Operations",
"description" : "MongoDB CRUD Operations",
"by" : "Java Guides",
"url" : "https://javaguides.net",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 20,
"comments" : [
{
"user" : "user1",
"message" : "My first comment",
"dateCreated" : ISODate("2013-12-10T09:35:00Z"),
"like" : 0
}
]
}
How to Find Indexes: getIndexes()
Finding an Index in MongoDB is done by using the "getIndexes()" method.
Syntax
db.collection_name.getIndexes()
Example
> db.posts.getIndexes();
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "mydb.posts"
},
{
"v" : 2,
"key" : {
"title" : 1
},
"name" : "title_1",
"ns" : "mydb.posts"
}
]
The output shows that we have two indexes in this collection. The default index created on _id and the index that we have created on title field.
How to Drop Indexes: dropIndex()
Removing an Index in MongoDB is done by using the "dropIndex()" method.
We can either drop a particular index or all the indexes.
Dropping a specific index
db.collection_name.dropIndex({index_name: 1})
Let's drop the index that we have created on the title field in the collection posts. The command for this:
> db.posts.dropIndex({title: 1});
{ "nIndexesWas" : 2, "ok" : 1 }
nIndexesWas: It shows how many indexes were there before this command got executed ok: 1: This means the command is executed successfully.
Dropping all the indexes:
To drop all the indexes of a collection, we use dropIndexes() method.
Syntax of dropIndexs() method:
db.collection_name.dropIndexes()
Let's drop all the indexes of posts collection.
> db.posts.dropIndexes();
{
"nIndexesWas" : 1,
"msg" : "non-_id indexes dropped for collection",
"ok" : 1
}
Summary
The below diagram shows the summary of all the commands used in this post:
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Comments
Post a Comment
Leave Comment