3

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?

1 Answer 1

5

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})
Sign up to request clarification or add additional context in comments.

1 Comment

Just to add to this, the index {a:1,b:1,c:1} will behave just like {a:1,b:1} (in addition to supporting c) since mongo will use index prefixes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.