Now the last interesting index option I want to show to you is a time to live index and that's a really
cool kind of index that can be very helpful for a lot of applications where you have self-destroying
data,
let's say sessions of users where you want to clear their data after some duration or anything like
that.
Let's create a new sessions collection here, you can of course come up with any example you want and
in there, I'll insert one document and that one document will receive some data
which is some random text
and more importantly, it will get a createdAt key and you can name this however you want, doesn't have to
be named createdAt. Now
this will receive a new date and it will receive new date like this which will just be the current timestamp
or the current date,
so if I look into sessions with find pretty, well then I see this ISO date was created by mongodb and
this is the current date and time.
Now let me add a time to live index here, for that,
I'll go to my sessions and create an index
and now I will create that index on the createdAt field.
Now first of all, you could of course create a normal ascending index here too and you can order dates
just like you can text and numbers but now I'll get rid of this because I want to add this index a bit differently.
Instead of adding it as I did before in ascending order,
I was still add it like this but I will add an additional argument here to configure this index and
there, I will add the expire after seconds field.
This is a special feature mongodb offers and that only works on date indexes or on date fields, on other
fields it will just be ignored,
you could add it but it will be ignored
and there I could say every element should be removed after 10 seconds.
Now if I hit enter here and I look into my collection, you'll see this value is still there because it
was there before the index was added and the index does not delete elements in hindsight
but if I insert a new element here, you see it's now in the database
but let's now wait for 10 seconds because that is the duration
I did specify and thereafter you see,
actually both were deleted.
The reason for that is
adding a new element basically triggered mongodb to now re-evaluate the entire collection,
so also the already existing documents and see whether this field which is indexed fulfills this criteria
of only being valid for 10 seconds
and therefore then it also reconsidered the existing documents,
it just doesn't do that right
when you add the index but it does do it whenever you add a new element.
So this can be very useful because it allows you to maintain a collection of documents which destroy
themselves after a certain time span and for a lot of applications,
this can be useful things like for example as I just said, session data for a user of your website
or maybe in an online shop where you want to clear the cart after one day,
so whenever you have a use case where data should clean up itself, you don't need to write a complex
script for that,
you can use a time to live index with that expiry after seconds addition we added in our index.
Important to know here by the way,
you can only use that on single field indexes,
it does not work on compound indexes and as I mentioned, it works on date objects.