Create Indexing in MongoDB via Terminal

Introduction: Indexing are used to swiftly locate data without having to search every row in a database table every time a database table is accessed

Note: I will you to recommend using indexing before putting the data into your database

Let's start with opening the terminal:

go to your MongoDB (where you installed the MongoDB Compass)bin folder and copy the folder path up to the bin and paste in terminal

Run the command:


Now run these commands one by one

  1. show “dbs” that will show databases that are present in your MongoDB

2. now select the database by command “use <database_name>”

3. now run this to create an index in the collection

index db.<collection_name>.createIndex(<indexVariable>:<indexVariableValue>)

Thank you😎!!!!!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vineet Mishra

Vineet Mishra

Full Stack Developer at CCR Tech, I write about Mobile/Web Development and Deployment, and Interview Q & A