You are reading wrong
You are .ensureIndex() quoted block there, which .ensureIndex() (now deprecated, but still called mongoose code) is actually here in context.
In the mongoose, you define an index at the schematic or model level that matches your design. What "mongoose" "automatically" does for you, it connects to each registered model, and then calls the corresponding .ensureIndex() methods for the provided pointers.
What is this actually doing?
Well, in most cases, after you have already launched your application before the .ensureIndexes() method has been started, this is Absolutely Nothing. This is a little exaggeration, but it is more or less true.
Because the index definition is already created in the server collection, the subgrid call does nothing. Ie, it does not drop the index and "recreates". Thus, real value is basically nothing once the index itself has been created.
Creating Indexes
So, since the mongoose is just a layer on top of the standard API, the createIndex() method contains all the details of what is happening.
There are some details, for example, that the assembly of the index can happen in the background, and although it is less intrusive for your application, it comes at its own expense. It is noteworthy that the size of the index from the generation of the "background" will be larger than if you built it in the foreground, blocking other operations.
In addition, all indexes come at the expense of cost, especially in terms of disk usage, as well as the additional cost of recording additional information outside the collection data itself.
The advantage of the index is that it is much faster to “search” for the values contained in the index than to search the entire collection and meet possible conditions.
These are the main “trade-offs” associated with indexes.
Deployment pattern
Let us return to the cited block from the documentation, for this advice there is a real intention.
In deployment templates, and especially with data migrations, actions in this order are characteristic:
- Filling data with relevant collections / tables
- Include collection / table data indexes appropriate to your needs.
This is due to the fact that there are costs associated with creating the index, and, as mentioned earlier, it is advisable to get the most optimal size from the index assembly, as well as to avoid that each document insert also had overhead for writing the index record when you do this "load" in the mass.
So, what indexes are for, these are costs and benefits, and the message in the manga documentation is explained.
In general, I suggest reading Database Indexes for what they are and what they do. Consider going to the library to find a book. At the entrance there is a map index. Do you go around the library to find the right book? Or are you looking at it in a card index to find where it is? This index took some time to create as well as keep it updated, but it saves “you” by walking throughout the library so you can find your book.