Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb If add new Index to already existing Collection

I would like to know if I add new index the older documents of a collection will be indexed as I added this new index rule?

like image 921
Alvaro Joao Avatar asked Mar 02 '16 20:03

Alvaro Joao


People also ask

Can we update index in MongoDB?

Modify an IndexTo modify an existing index in the MongoDB Shell, you need to drop and recreate the index. The exception to this rule is TTL indexes, which can be modified via the collMod command in conjunction with the index collection flag.

Do we need to rebuild index in MongoDB?

According to MongoDB documentation: Normally, MongoDB compacts indexes during routine updates. For most users, the reIndex command is unnecessary. However, it may be worth running if the collection size has changed significantly or if the indexes are consuming a disproportionate amount of disk space.

How many indexes can be created on a collection in MongoDB?

A collection cannot have more than 64 indexes. The length of the index name cannot be longer than 125 characters. A compound index can have maximum 31 fields indexed.

How do I add a new field to an existing record in MongoDB?

To add field or fields to embedded documents (including documents in arrays) use the dot notation. See example. To add an element to an existing array field with $addFields , use with $concatArrays .


1 Answers

Yes, all documents including existing one will be indexed. Once you create an index, MongoDB builds index from scratch.

It's recommended to create index during off peak hours if you have very large collection as it will block all other operations on collection being indexed, unless it's not a background index operation.

see https://docs.mongodb.org/manual/reference/method/db.collection.createIndex/#behaviors

like image 63
Saleem Avatar answered Nov 30 '22 04:11

Saleem