MongoDB tutorials

8. Capped collections

Video tutorial:

Capped collection is a type of collection which automatically delete old documents when new ones are inserted. If you remember the old days when your text message memory becomes full and you have to delete old messages in order to free space for new messages. Capped collections does that automatically:

no-space-for-new-messages

A collection can be made capped at the time of creating:

db.createCollection("users", { capped: true, size: 10000, max: 3 })

size means the bytes if the collection exceeds that the number of bytes then it will start removing old documents. max means the number of entries, in this case, if the collection has more than 3 documents then it will start deleting old documents. Now it is your task to insert 3 documents in users collection, as soon as you insert the 4th document, the 1st document will automatically be removed.