0
votes

I'm reading MongoFB documentation. At this url http://docs.mongodb.org/manual/tutorial/build-indexes-on-replica-sets/ I read "Create the new index using the ensureIndex() in the mongo shell, or comparable method in your driver. This operation will create or rebuild the index on this mongod instance"

I understand well? If the index is already present, does mongoDB perform a rebuild?

So the difference with "reIndex()" is that reIndex() performa a rebuild on all the indexes of a collection. Is it correct?

1
I think that might be an English error, ensureIndex() registers a no-op if the index already exists, or at least shouldSammaye

1 Answers

2
votes

I believe that is either an English mistake or talking about if you are rebuilding indexes on replica sets either way the documentation for ensureIndex() specifically states:

http://docs.mongodb.org/manual/reference/method/db.collection.ensureIndex/#behaviors (the one linked to from here: http://docs.mongodb.org/manual/tutorial/build-indexes-on-replica-sets/#build-the-index )

If you call multiple ensureIndex() methods with the same index specification at the same time, only the first operation will succeed, all other operations will have no effect.

So calling it again should not result in a rebuild unless you are rebuilding your indexes.

If you want to rebuild the index you must drop it first and then rerun ensureIndex():

To add or change index options you must drop the index using the dropIndex() method and issue another ensureIndex() operation with the new options.

If you create an index with one set of options, and then issue the ensureIndex() method with the same index fields and different options without first dropping the index, ensureIndex() will not rebuild the existing index with the new options.