Reputation: 10658
Consider I have insert a dozen of documents of this format:
{"name":"some one", "age":32}
and then I build the index on the "name" property using the "unique" parameter.
When I insert a new document of the same format, do I need to rebuild the index on name after every insert or this is automated? Also upon new insertion with same name, because of the unique param, would the old document be deleted? Or the insertion just won't happen?
Using mongo 2.4.12 btw.
Cheers
Upvotes: 2
Views: 1045
Reputation: 6371
No you don't need to create index again
if you use
db.collection.ensureIndex( { field_to_be_index: 1 }, { unique: true, dropDups: true } )
it will drop the duplicate document on creating index.
See the full documentation of duplicate dropping for more information.
WARNING: as the document says, Specifying { dropDups: true } may delete data from your database. Use with extreme caution.
after creating the unique index, if you index with non-unique filed, you will receive duplicate key error index: ...
error
Upvotes: 2