Back I am in the shell, still connected to my locally running mongod process.
Now I want to introduce you to capped collections and explain what capped collections are. Capped collections
are a special type of collection which you have to create explicitly where you limit the amount of data
or documents that can be stored in there and old documents will simply be deleted
when well this size is exceeded,
so it's basically a store where oldest data is automatically deleted when new data comes in.
This can be efficient for high throughput application logs where you only need the most recent logs
or as a caching service where you cache some data and if the data then was deleted because it
hasn't been used in a while, well then you're fine with that and you can just re-add it. To create such a
collection,
first of all you need to use a database of course,
I'll use a new database, performance
and then you create it with the create collection command, we saw that when we added validation earlier
in the course.
Now first of all, you define a name and I'll just name it capped but of course, the name is totally up to
you,
this collection can be anything, can also be users, whatever you like,
so I'll name it capped. The important part
here are now the options,
so the document you pass as a second argument to create collection. Here, the option you want to set is
capped to true
and this will turn this into a capped collection. Now by default, it will have a size limit of 4 bytes but
you can set size to any other value and it will then automatically be turned into a multiple of 256 bytes.
So you could for example set this to 10000 bytes,
it will be converted a bit and that will be your size and you can also add a Max key,
the size is required, Max is optional
and Max allows you to also define the amount of data that can be stored in there, measured in documents,
so I could cap this to three documents at most.
With that, I hit enter and I get this collection and now I can access capped as a collection
and in that capped collection, we can now of course insert a document just as before. We can insert a document
named Max and
this worked.
I can now also add Manu and this worked
and I can also add Anna and this worked.
Now I have three documents in there which is our max size.
I can now of course also find my documents like this and I get back Max, Manu and
Anna. The important thing is for a capped collection, the order in which we retrieve the documents is always
the order in which they were inserted. For a normal collection,
that may be the case but it's not guaranteed,
so for a normal collection, you always need to sort if you want to sort for example by ID.
You can do that and then you'll sort by that index but by default, in a capped collection, you'll always get
the insertion order. If you want to change the order and sort and reverse,
there is a special key you can use and that is $natural, the natural order by which it is
sorted and then for example use -1 and then it's sorted the other way around,
just as a little side note, this is important to understand or to keep in mind.
You also can create indexes in there and you got an index on ID by default
but of course you don't have to. Now with that, I found my documents,
now let's insert another document,
let's insert Maria and keep in mind that our size limit was three.
Now if I insert Maria, I don't get an error because the idea of a capped collection is not that it gives
me an error when I insert too many documents but that it clears the oldest document which in my case
should be Max because that was the first document I added.
So if I now output all entries again, indeed we see Max is gone, there's only Manu,
Anna and Maria.
So this is a capped collection and there are some use cases where this can be a good idea,
why would you use a capped collection instead of a normal collection then?
Well because of the automatic clear up. You can keep this collection fairly small and therefore, efficient
to work with
and you don't have to worry about manually deleting old data,
so for use cases where you need to get rid of old data anyways with new data coming in or where you
need high throughput and it's ok if you lose old data at some point, like for caching, then the capped
collection is something you should keep in mind as a tool to improve performance.
Obviously this is not a solution for storing anything like products or users or blog posts,
that would be horrible because you would simply lose some users, some products or some blog posts.