Reputation: 2285
Say, we want to add index to a zipcode
field of mongodb collection people
. In order not to affect any operations, we write the following line: db.people.createIndex( { zipcode: 1}, {background: true} )
. Now, I'm having a hard time understanding what exactly does that do?
It's a command to create and index. When we specify {background: true}
, does that mean that it will run in background only on the index's initial creation (after we press enter), or every time new record is added?
Upvotes: 1
Views: 3321
Reputation: 262534
Background index creation starts immediately (when you "press enter"), but it will be done in the background and you can continue updating the collection while this is being done.
Any documents you add while index creation is still ongoing will make it into the final index, but this will not happen immediately when you insert the document (it also happens "in the background" if you will, but really the index does not properly exist yet at this point).
Once the index has been fully created (i.e. is up-to-date with the collection), it works just like a normal index.
That means adding new documents to the collection will also add them to the index at the same time (not sometime later).
Upvotes: 4