How do you create an index in MongoDB?

Your thoughts?

|

Creating an index in Mongo is easy...

db.myCollection.createIndex( { name: 1 }} )

Understanding how indexes work and the nuances surrounding the different types, their use case, indexing strategies, etc is the hard part (the part people get paid to understand)

If you want to know how to create an index, you need to also know what TYPE of index you are creating...

Creating a single field index:

db.myCollection.createIndex( { name: 1 }} )

This creates an index on a single field "name" in ascending order 1 (vs descending order -1)

So now when you do something like...

db.myCollection.find({name:"Erick"})

Mongo doesn't need to traverse or scan the entire "myCollection" to find what it needs. Instead, it can consult a more compact ordered list (stored as a B-tree) to quickly find what you need.

Creating a compound index:

db.myCollection.createIndex( { name: 1, email:-1 }} )

This is similar to a single field index, except it sorts by name AND THEN by email.

The fun doesn't stop there. There are also multikey, unique, geospatial, partial, hidden, TTL etc. but this covers the basics of how you create an index and the benefits they add...

|

there are a few different ways to create an index in MongoDb. You can use the driver class for whatever library you are using..for example if you are using the MongoDb driver with NodeJS you can do...

const database = client.db("my_db");
const myCollection = database.collection("myCollection");
const result = await myCollection.createIndex({ email: 1 });
console.log(`Index created: ${result}`);

Or if you are using Mongo Shell it would be something like this...

db.myCollection.createIndex( { email: 1 }, { collation: { locale: "fr" } } )
|

Create a single field index:

db.someCollection.createIndex({email:1})

Create a compound index:

db.someCollection.createIndex({email:1, name:-1})

Create a text index:

db.someCollection.createIndex({comments:"text"})

Create a wildcard index:

db.someCollection.createIndex({"userInfo.$**":1})

Create a hashed index:

db.someCollection.createIndex({"userInfo":"hashed"})
|

When creating an index its really important to understand how your database is going to work. By this I mean how will you interact with your database? What are the most popular queries, etc.

This is crucial because indexing is all about performance. You only want to create an index on the most commonly run queries on the most commonly run fields.

|

Creating an index in MongoDB is as simple as...

db.createIndex({email:1})

This creates an index on a single field email. The (1) indicates ascending order (-1 for descending)

The order doesn't really matter when you index a single field...however you can also do compound indexing...

db.createIndex({email:1, name:1})

This is a compound index. This satisfies the first index we created as well because of something called prefixes.

Long story short, creating indexes is easy but knowing when to create them and on what fields is the challenging part.

In fact, optimizing your database with efficient indexes is one of the most challenging parts of database management. While there are tools that will automate the indexing of your database by inspecting log output etc, it is really important to understand how these work.

It's also worth mentioning that creating indexes doesn't ALWAYS improve performance. Sometimes it can hurt queries more to have an index than to not have an index. You really need to be careful when creating these and understand the pain points of your data set and what the most frequently run queries are.

|

as per the docs:

db.collection.createIndex(keys, options, commitQuorum)
|

just do it brah!