Adding Index to Composite Index on Mongodb

I created a composite index on my db using:

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

      

Now I realized that I needed one more level in composition:

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

      

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

+3


source to share


1 answer


A call ensureIndex

with different fields will create a new index.

You can confirm this after running both commands with getIndexes

to find out what indexes exist for your collection:

db.collection.getIndexes()

      



If you no longer need the original index, you can remove it using:

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

      

+5


source







All Articles