MongoDB Index Builds – Preventing Users From Triggering New Builds
Tags: mongodb, indexes, users, database
Published: 2020-06-30 16:56:26

---
title: MongoDB Index Builds – Preventing Users From Triggering New Builds
published: true
description: How to prevent your users from accidentally triggering MongoDB index builds from the MongoDB CLI to stop them from bringing your cluster down - ScaleGrid Blog
tags: MongoDB, Indexes, Users, Database,
//cover_image: https://scalegrid.io/blog/wp-content/uploads/2020/06/MongoDB-Index-Builds–Preventing-Users-From-Triggering-New-Builds-ScaleGrid-Blog.jpg
---
<link rel="canonical" href="https://scalegrid.io/blog/mongodb-index-builds-preventing-users-from-triggering-new-builds/" />
<a href="https://scalegrid.io/blog/mongodb-index-builds-preventing-users-from-triggering-new-builds/"><img src="https://scalegrid.io/blog/wp-content/uploads/2020/06/MongoDB-Index-Builds–Preventing-Users-From-Triggering-New-Builds-ScaleGrid-Blog.jpg" width="100%" height="auto"></a>

<p style="text-align: justify; padding-bottom: 10px;">Index builds on MongoDB production clusters need to be handled with the utmost care. We have documented the issues in detail in one of  our previous blog posts - <a style="color: #2da964;" href="https://scalegrid.io/blog/the-perils-of-building-indexes-on-mongodb/" target="_blank">The Perils of Building Indexes on MongoDB</a>.</p>
<p style="text-align: justify; padding-bottom: 10px;">Depending on the size of your data, both foreground and background builds can bring your cluster down. So, how do you prevent your users from accidentally triggering index builds from the MongoDB CLI? The short answer is that you cannot. However, what you can do is remove the "CreateIndex" privilege from most of your users so that they cannot accidentally trigger an index build from the CLI. There should be very few users in your system who have access to write data to the database. Among these users, even fewer should have permission to build indexes. For a primer on how to use MongoDB roles, please refer to the documentation - <a style="color: #2da964;" href="https://docs.mongodb.com/manual/tutorial/manage-users-and-roles/" target="_blank">Manage Users and Roles</a>.</p>
<p style="text-align: justify; padding-bottom: 10px;">The best option to implement this is to create your own custom role and remove the ‘CreateIndex’ privilege for your users. However, we did not want to build the list of permissions by hand since this will be different for each context and possibly MongoDB version. We put together this small script to use one of the existing built-in roles, and removed the ‘CreateIndex’ privilege from this role. In this example, we are using the builtin "readWrite" role and removing the "CreateIndex' privilege from this role:</p>


```
var privs = db.getRole('readWrite',{ showPrivileges: true });
privs.privileges.forEach(function (item, index) {
  var index = item.actions.indexOf("createIndex");
  if (index !== -1) item.actions.splice(index, 1);
});
db.createRole({role:"readWriteNoIndex",privileges:privs.privileges,roles:[]});
```

<ol>
	<li style="text-align: justify; padding: 15px 0px 10px;">Save the contents of the code snippet as createRole.js.</li>
	<li style="text-align: justify; padding-bottom: 15px;">Run the script using the syntax below:</li>
</ol>

```
mongo -u <user> -p <password> <host>:27017/<db name> --authenticationDatabase admin createRole.js
```

<p style="text-align: justify; padding: 15px 0px 15px;">Once the role is created, you can use this role to create users going forward. Check out the full blog post: <a href="https://scalegrid.io/blog/mongodb-index-builds-preventing-users-from-triggering-new-builds/">MongoDB Index Builds – Preventing Users From Triggering New Builds</a></p>


<h3 style="font-size: 30px; padding-bottom: 20px;">More tips for you</h3>
<h4>How to Create Case-Insensitive Indexes in MongoDB</h4>
<p style="padding-bottom: 10px;">Case-insensitive indexes support queries that perform string comparisons without regard to the letter case. Collation gives you the ability to specify language-specific rules for string comparison. <a style="color: #2da964; text-decoration: underline;" href="https://scalegrid.io/blog/how-to-create-case-insensitive-indexes-in-mongodb/">Learn more</a></p>


<h4>How to Stop a Runaway Index Build in MongoDB</h4>
<p style="padding-bottom: 10px;">Index builds in MongoDB can have an adverse impact on the availability of your cluster.  If you trigger a foreground index build on a large collection, your cluster may be unresponsive until the index build is complete. <a style="color: #2da964; text-decoration: underline;" href="https://scalegrid.io/blog/how-to-stop-a-runaway-index-build-in-mongodb/">Learn more</a></p>


<h4>MongoDB Regex, Index & Performance</h4>
<p style="padding-bottom: 10px;">MongoDB supports regular expressions using the $regex operator. However these MongoDB regex queries have a downside,  all but one type of regex makes poor use of indexes and results in performance problems. <a style="color: #2da964; text-decoration: underline;" href="https://scalegrid.io/blog/mongodb-regular-expressions-indexes-performance/">Learn more</a></p>