Thursday, February 6, 2014

How to rebuild Indexes in MongoDB?

In case you want to rebuild your Index MongoDB provide the reIndex() method to do so. Sometime due to large data insertion or operation we need to rebuild the index. The syntax to rebuild all the index on a collection (table) is following:

db.collection_name.reIndex()

For example to rebuild all the Indexes of scores collection (table) we can issue the following command to MongoDB shell

db.scores.reIndex()

If you want to rebuild one specific index of collection you can use the below syntax:

db.collection_name.reIndex({field_name:1 or -1,field_name2: 1 or -1})

For example to rebuild index Smpname_1_Salary_-1 from employeeList collection we can pass the following command to MongoDB shell:

db.employeeList.reIndex({EmpName:1,Salary:-1})



Popular Posts

Blog Archive

Real Time Web Analytics