That's it for this module, we had a very detailed look at indexes. Indexes allow us to retrieve our data
more efficiently,
mostly we saw some exceptions to this module but generally, they speed up our queries and also the sorting.
We can use single field compound multikey and text indexes,
now there are a couple of other indexes and for example geospatial indexes are something we'll see in
the geospatial module
but these are the most important indexes for now and it's also important to understand that indexes don't
come for free,
they will slow down your writes,
that is something you always have
but this still worth the effort
but this is something you should keep in mind because it means that you should not create the index
for every single field,
this will be inefficient because all these indexes need to be updated with every insert, update or delete.
We can use indexes for querying and sorting and compound indexes have this special behavior where you
can use them left to right,
if you have a compound index made up of name and age, you can either use that index to optimize your queries
where you look for name and age with some criteria or also for queries that just use the name, just the
age is not possible due to the way this is stored.
To find out whether your queries and indexes do their job in a good way,
you can diagnose your queries with the explain method,
also remember the options you could pass to explain, query planner, execution stats and all plans execution.
The output there gives you a detailed look into what mongodb did and how it derived
this plan it used and it also gives you an idea of how you might optimize your indexes,
for example if you have a large gap between the keys it had to look at and the documents it actually
retrieved. When creating your indexes,
you can also configure them,
for example you can add a time to live if the index is placed on a date time
or you can turn them into unique or partial indexes. For text indexes,
you can also assign weights and a default language.
Now this is what we covered in this module and this hopefully helps you create efficient databases with
efficient indexes and then writing efficient queries for that.