How to Find Unused Indexes in MongoDB?
Tags: mongodb, indexstats, nosql, collection
Published: 2018-02-06 18:56:48

---
title: How to Find Unused Indexes in MongoDB?
published: true
description: In this post, we’ll show you how to find unused indexes in MongoDB, check usage statistics on your index collection, and how to drop your unused indexes.
tags: MongoDB, IndexStats, NoSQL, Collection
canonical_url: https://scalegrid.io/blog/how-to-find-unused-indexes-in-mongodb/
---
<link rel="canonical" href="https://scalegrid.io/blog/how-to-find-unused-indexes-in-mongodb/" />

<p style="text-align: justify;">In this post, we'll show you how to find unused indexes in MongoDB, check usage statistics on your index collection, and how to drop your unused indexes.</p>
<p style="text-align: justify;">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 are 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.</p>
<p style="text-align: justify;">Indexes have a big impact on write performance - every time there's a write to a collection, the relevant indexes need to be updated. 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 - <a href="https://scalegrid.io/blog/the-perils-of-building-indexes-on-mongodb/">Perils of building indexes on MongoDB</a>.</p>
<p style="text-align: justify;">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 <span style="color: #283037;"><a href="https://docs.mongodb.com/manual/reference/operator/aggregation/indexStats/">$indexStats</a> operator which lets you gather statistics about your index usage.</span></p>

<h2 style="text-align: justify;">Finding Unused Indexes in MongoDB</h2>
<p style="text-align: justify;">To check usage statistics of a particular index on a collection, you can use this command:</p>


1  db.collection.aggregate([{$indexStats: {}}, {$match: {"name": "&amp;amp;lt;indexname&amp;amp;gt;"}}])


<p style="text-align: justify;">To get the statistics for all the indexes of a collection:</p>


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


<p style="text-align: justify;">The returned document will look like this:</p>


1  {
2  "name" : "test",
3  "key" : {
4  "key" : 1
5  },
6  "host" : "xxx:27017",
7  "accesses" : {
8  "ops" : NumberLong(145),
9  "since" : ISODate("2017-11-25T16:21:36.285Z")
10  }
11  }


<p style="text-align: justify;">The two important fields to note here are:</p>
<ol>
	<li>
<h4 style="margin-bottom: -5px;">Ops</h4>
<p style="text-align: justify;">This is the number of operations that used the index.</p></li>
	<li>
<h4 style="margin-bottom: -5px;">Since</h4>
<p style="text-align: justify;">This is the time from which MongoDB gathered stats, and is typically the last start time.</p></li>
</ol>

<h2 style="text-align: justify;">Dropping Unused Indexes in MongoDB</h2>
<p style="text-align: justify;">Once you've identified and verified unused MongoDB indexes, you can drop the index using the code below:</p>


1  db.collection.dropIndex( "&amp;amp;lt;index name&amp;amp;gt;")&amp;amp;nbsp; or
2  db.collection.dropIndex("{key1:1.....}")


<p style="text-align: justify;">As always please verify that you are dropping the right index before you proceed with the drop operation.</p>

<p style="text-align: justify;">If you have any questions on finding unused indexes in MongoDB, leave a comment below or reach out to us at <a href="mailto:support@scalegrid.io">support@scalegrid.io.</a></p>
<p>&nbsp;</p>