We're almost done with this module,
one last word about how you build indexes and with this, I really mean how you add them. You can add them in two
ways,
in the foreground and in the background. Thus far in the module, we always added indexes in the foreground
with create index just as we executed it,
now actually something we didn't notice because it always happened instantly basically,
actually during the creation, the collection will be locked and you can't edit it,
on the other hand, you can also add indexes in the background and the collection will still be accessible.
The advantage of the foreground mode is that it's faster,
the background is slower but if you have a collection that is used in production, you probably don't
want to lock it just because you were adding an index,
so let me show you how you can add an index in the background and which difference that makes. To show the
difference that this makes,
I prepared a javascript file which you find attached to this video,
the shell can also execute such files by simply typing mongo and then the file name, credit-rating.js
is the file.
Now what this will do is that mongo still connects to the server and then it will execute the file
and basically execute the commands in there against the server and in that file,
I essentially have a loop that will add 1 million documents to a collection with random numbers in there.
Now executing this will take quite a while, can take multiple minutes depending on your system,
you can always quit it with control c and you can also of course edit the code to reduce the number
of documents that are created.
Now I will leave that running and I'll be back once it is finished,
again don't worry if it takes a while,
that is totally normal.
So this completes successfully for me and I can now connect with the mongo command and on the server,
let's run show dbs and it's the credit database that was added
now so let's use credit and in there, if you show the collections, you should have a ratings collection
and if you count that, you should see that there are 1 million documents in there. Now
nothing in there is indexed
and before we start adding an index in the back or foreground, we can take this as an opportunity to
practice indexing a bit more.
So let's have a look at ratings and find one element only,
this is how a single element looks like, we get an age in there and we get a score for the person and let's now say
we want to for example index age.
So we can create an index on age in an ascending order and you already see, this now doesn't finish instantly, really
quick still but not instantly because we have a million documents which have to be indexed
and now let's use that index. For that, I'll use my ratings and I will also explain the results with execution
stats
and now let's find all ages where people let's say are older than 80.
Now here you see close to 100000 keys were examined because this is totally randomly generated
and we used an index scan here, where we had to look at all these keys, all the docs and return these docs,
took 200 milliseconds.
Now let's drop that index
with db ratings drop index
and we had an index on the age in ascending order so
let's drop that and let's rehit this query and there we can already see, this took longer, like 400 milliseconds,
so double as long.
So the index did already pay off there
but of course this is already something you know,
now I wanted to show you that the index creation takes time.
Now remember I just deleted my index right, so I only have the default index on there right now,
so now I will actually create an index and I will show you that this will block us from doing anything
with this collection. For that,
I will open a new tab in which I also connect to my mongo shell and I will already prepare a query
there,
so now I'm connected to the same database and there, I will prepare ratings, find one.
Now I need to switch quickly later because it doesn't take that long but I'll try to execute this whilst
the index is getting created.
So now I will create that index on the age again,
so I hit enter
and you see, this doesn't finish here,
it takes a while, it only finishes after this creation is finished essentially.
If I drop that index and I prepared to create that again and here, I try to insert one document with
let's say a person Id, some random text which of course is not in line with the other IDs which are numbers
but it doesn't matter,
some score and an age of let's say 90, so I prepared this,
I want to insert that person and again create that index and try this,
it doesn't finish,
it only finishes after the index creation is done,
so after the collection is unlocked again.
So it's not getting an error here,
it is working in the end but whilst the index is getting created,
it's not working,
it's basically deferred until the index is created.
Now our index creation here is still going fairly fast,
it didn't take much longer than a second probably but it's not hard to imagine that for more complex indexes
like a text index, let's say
or for even more documents and more complex documents, that index creation will easily take longer but
especially more complex things like text indexes will take much longer and then this will be a problem
because then the database or the collection might be locked for a couple of minutes even. So this is
not an alternative for a production database,
you can't suddenly lock down your entire database and your application is not able to interact with
it anymore and that is why you can create it in the background.
So let me again prepare this insertion command here
and let me go back and first of all drop the index and then prepare the creation again
but now with a second argument where we previously sets things like the default language,
now I will set background to true.
The default is false which means it's created in the foreground
but if you set this to true, it will be created differently in a way where the collection doesn't need
to be locked.
So now if I do this and I hit enter here and I hit enter here, you see it's inserted immediately
even though the index creation still blocks this thread, we couldn't enter any other arguments but technically,
it happened in the background without locking the collection.
So this is a very useful feature for production databases,
you don't want to add an index in the foreground there especially not if it will take quite a while.