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 para­met­ers.

What is MongoDB Create Index?

We’ll begin by taking a look at the structure of MongoDB to help us to un­der­stand what the MongoDB Create Index function is used for and why it is so important for working with the Database Man­age­ment System. The NoSQL solution does not rely on tables, like re­la­tion­al systems such as MySQL, relying on documents stored in col­lec­tions instead. These do not have to follow a schema, rather they are scalable and very flexible. This allows huge databases and col­lec­tions 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 col­lec­tion with thousands of documents is time consuming and frus­trat­ing. 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 Mon­goD­B­Cre­ate Index method consists of the col­lec­tion display, the actual command, the criterion for search queries, and a counting direction:

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

“COL­LEC­TION_NAME” indicates the name of the col­lec­tion in which the index should be created. “cre­ateIn­dex” is the cor­res­pond­ing command. “KEY” indicates the field within the col­lec­tion, 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 un­der­stand the func­tion­al­ity and use­ful­ness 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 col­lec­tion was created manually using MongoDB Create Col­lec­tion. 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 par­en­theses after the keys. The following options are available:

  • back­ground: This parameter creates the index in the back­ground and prevents in­ter­fer­ence with other database activ­it­ies. 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 auto­mat­ic­ally 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 in­ac­cur­ate. The default value is “false”.
  • ex­pire­After­Seconds: You decide how long MongoDB keeps documents in a col­lec­tion with this option. The value is specified in seconds.
  • weights: This parameter de­term­ines the im­port­ance 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")
Go to Main Menu