DeanLa
DeanLa

Reputation: 1919

Adding index on a compound index on Mongodb

I created a compound index on my db using:

db.collection.ensureIndex({a:1,b:1})

Now I realized I need another level in the composition:

db.collection.ensureIndex({a:1,b:1,c:1})

Will Mongodb create a whole new index, or will it know to modify the existing one?

Upvotes: 3

Views: 91

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 311835

Calling ensureIndex with different fields will create a new index.

You can confirm this after running both commands by using getIndexes to see what indexes exist for your collection:

db.collection.getIndexes()

If you no longer want the original index, you can drop it using:

db.collection.dropIndex({a:1,b:1})

Upvotes: 5

Related Questions