MongoDB Create index using Mongo Compass
In this article, we shall see how to perform the MongoDB Create index in the MongoDB collection using Mongo Compass UI or mongo shell tools.
Today in this article, we will cover below aspects,
We already looked at creating indexes using Mongo Shell in the previous article.
There are many advantages of indexing if used appropriately,
- Indexing supports the efficient execution of queries.
- It’s the best means of controlling the database scan.
- Let’s you control the scan of every document efficiently. Please note without indexes each and every document and will do a full scan in a collection.
- Indexing helps to limit the number of documents it must inspect.
- It provides the ability of a query to narrow results using the index
- Indexes increased query performance.
If you are interested in using the Mongo Shell CLI tool to create indexes, drop indexes please refer to the below article,
Create MongoDB index – Mongo Compass UI
To create indexing please log in to the MongoDB database using mongo compass.
Click on the Indexes menu in the UI after selecting the collection,
You can see any existing indexes on the Index page.
To add new indexes please click on the Create Index.
Let’s now create index in mongodb based on your query requirements.
Below I am creating indexes for “DateAdded” field.
The above commands of indexing are equal to the below createIndex() methods if executed using the mongo shell.
Example:
db.Books.createIndex({'DateAdded':1})
db.CreditScore.createIndex({'CreditScore':1})
Additionally one can specify below options on creating indexes in the background,
Foreground Vs Background Build Indexes
- Foreground index builds required blocking read-write access to the parent database of the collection being indexed for the duration of the build.
- Foreground indexes are faster efficient.
- Background index builds allows read-write access to the database and its collections during the build process.
- Background indexes are slower and less efficient but very convenient to use.
In the recent version of Mongo DB Background indexes are made much more efficient and are convenient to use.
A unique index ensures uniqueness for the indexed fields, similar to the _id field created uniquely by default by MongoDB during the creation of a collection.
But if you need to create uniqueness for a custom-created index please use this option.
Create index in Mongodb – Compound index
Below is the basic way for Creating Compound Indexes using Mongo compass.
Below will create the required compound indexes as below,
Indexes are resource-intensive and each index occupies space in the database. It should be created if the query demands it and uses it extensively.
One should follow all possible Best practices for MongoDB Indexes
References :
Do you have any comments or ideas or any better suggestions to share?
Please sound off your comments below.
Happy Coding !!
Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.