Articles → MONGODB → Create An Index In Mongodb

Create An Index In Mongodb






Purpose





Syntax


// Create index in ascending order
db.[collection].createIndex({ field_name: 1 })

// Create index in descending order
db.[collection].createIndex({ field_name: -1 })



Example


Picture showing creating an index using createIndex method



Dropping An Index




db.[collection].dropIndex("index_name")


Picture showing dropping an index using dropIndex method



Get The List Of Indexes




db.[collection].getIndexes()


Picture showing getting the list of index using the getIndexes method



Posted By  -  Karan Gupta
 
Posted On  -  Monday, April 22, 2024

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250