DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Loading XML into MongoDB
  • Mastering Full-Stack Development: A Comprehensive Beginner’s Guide to the MERN Stack
  • How To Perform Data Migration in MongoDB Using Node.js
  • Leveraging AI and Vector Search in Azure Cosmos DB for MongoDB vCore

Trending

  • Using Python Libraries in Java
  • What’s Got Me Interested in OpenTelemetry—And Pursuing Certification
  • Developers Beware: Slopsquatting and Vibe Coding Can Increase Risk of AI-Powered Attacks
  • Proactive Security in Distributed Systems: A Developer’s Approach
  1. DZone
  2. Data Engineering
  3. Databases
  4. How to Find Unused Indexes in MongoDB

How to Find Unused Indexes in MongoDB

Learn how to find unused indexes in MongoDB, check usage statistics on your index collection, and drop your unused indexes.

By 
Dharshan Rangegowda user avatar
Dharshan Rangegowda
·
Feb. 08, 18 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
7.7K Views

Join the DZone community and get the full member experience.

Join For Free

In this post, we'll show you how to find unused indexes in MongoDB, check usage statistics on your index collection, and drop your unused indexes.

Actively managing MongoDB database indexes over several application releases can be challenging for development teams. Requirements often change with every release, where several new indexes may be added and old indexes might become abandoned. Over time, this makes it difficult to keep track of which MongoDB indexes are being used and which ones are now unnecessary.

Indexes have a big impact on write performance — every time there's a write to a collection, the relevant indexes need to be updated. A lack of indexes manifests immediately and slows down the query — unused indexes are more subtle and need to be actively pruned out to improve write performance. More information can be found here.

In earlier versions of MongoDB, there was no easy way to determine if an index was not being used. However, starting in version 3.2, MongoDB added support for the $indexStats operator, which lets you gather statistics about your index usage.

Finding Unused Indexes in MongoDB

To check usage statistics of a particular index on a collection, you can use this command:

db.collection.aggregate([{$indexStats: {}}, {$match: {"name": "<indexname>"}}])

To get the statistics for all the indexes of a collection:

db.collection.aggregate([{$indexStats:{}}])

The returned document will look like this:

{
"name" : "test",
"key" : {
"key" : 1
},
"host" : "xxx:27017",
"accesses" : {
"ops" : NumberLong(145),
"since" : ISODate("2017-11-25T16:21:36.285Z")
}
}

The two important fields to note here are:

  1. Ops: This is the number of operations that used the index.

  2. Since: This is the time from which MongoDB gathered stats, and is typically the last start time.

Dropping Unused Indexes in MongoDB

Once you've identified and verified unused MongoDB indexes, you can drop the index using the code below:

db.collection.dropIndex( "<index name>")  or
db.collection.dropIndex("{key1:1.....}")

As always, please verify that you are dropping the right index before you proceed with the drop operation.

MongoDB

Published at DZone with permission of Dharshan Rangegowda, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Loading XML into MongoDB
  • Mastering Full-Stack Development: A Comprehensive Beginner’s Guide to the MERN Stack
  • How To Perform Data Migration in MongoDB Using Node.js
  • Leveraging AI and Vector Search in Azure Cosmos DB for MongoDB vCore

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!