How to use MongoDB Create Index

MongoDB Create Index function creates indexes. Indexes give users a better overview of their documents and make it easier to find what they need. You can customise an index with various parameters.

What is MongoDB Create Index?

We’ll begin by taking a look at the structure of MongoDB to help us to understand what the MongoDB Create Index function is used for and why it is so important for working with the Database Management System. The NoSQL solution does not rely on tables, like relational systems such as MySQL, relying on documents stored in collections instead. These do not have to follow a schema, rather they are scalable and very flexible. This allows huge databases and collections to be created and documents to be inserted. You’ll need some kind of order find certain data or data sets, and this is achieved with indexes.

An index improves order in a database and makes it easier for the system to process search queries. Without this, searching a huge collection with thousands of documents is time consuming and frustrating. Equipping the documents with indexes means they will be well-organized and easier to locate. The search is more efficient as only a part of the documents has to be read. You can use the MongoDB command Create Index to do this.

What is the MongoDB Create Index syntax?

The MongoDBCreate Index method consists of the collection display, the actual command, the criterion for search queries, and a counting direction:

> db.COLLECTION_NAME.createIndex ( {KEY:1} )

“COLLECTION_NAME” indicates the name of the collection in which the index should be created. “createIndex” is the corresponding command. “KEY” indicates the field within the collection, and the “1” indicates that sorting should be done in ascending order. Select “-1”for the opposite direction.

MongoDB Create Index example

Let’s take a look at a simple example to better understand the functionality and usefulness of Create Index:

> db.example.createIndex ( {"title":1} )

The output will look like this:

{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter": 2,
"ok" : 1
}

The first line indicates that the collection was created manually using MongoDB Create Collection. The second line shows how many indexes there are before the command is carried out. The third line shows the number of indexes after the command and the fourth line indicates if the command was a success.

What are the options for MongoDB Create Index functions?

There are several options available in MongoDB to optimize the Create Index function. The options are placed is inside the parentheses after the keys. The following options are available:

  • background: This parameter creates the index in the background and prevents interference with other database activities. This option always contains a Boolean value, so it is either “true” or “false”. The default value is “false”.
  • unique: “unique” is also a Boolean value. If this is “true”, a unique index is created using Create Index, which does not include any documents whose key value is already being used. This means that each key value occurs only once. The default value of unique is also “false”.
  • name: You can give the index any name with this option. The system will generate a name automatically if you leave this parameter out.
  • sparse: If you set this value to “true”, the index will only consider documents with a clearly defined field. This method saves space, but it can also be inaccurate. The default value is “false”.
  • expireAfterSeconds: You decide how long MongoDB keeps documents in a collection with this option. The value is specified in seconds.
  • weights: This parameter determines the importance of a field compared to other fields in an index. The value ranges from 1 to 99999.
  • default_language: You can specify the “default_language” if you want to use a language other than English for the MongoDB command Create Index.
  • language_override: You can override the default language with this option.

How do I delete indexes with MongoDB Drop Index?

You should know now how to create a new index with Create Index, the only question that remains is how to remove an index. The MongoDB Drop Index function is what you can use in this case. You have two options. You can either specify the file which the index should be removed from, or you can specify the name of the index.

This is the syntax of the first method:

> db.COLLECTION_NAME.dropIndex ( {KEY:1} )

The syntax of the second method is as follows:

dropIndex ("name_of_index")
Was this article helpful?
Page top