Questions
What are capped collections and when would you use them in MongoDB?
The Scenario
You are a backend engineer at a social media company. You are building a new service that needs to store a log of the most recent user activity.
You need to find a way to store the logs in a way that is efficient and that automatically removes old logs.
The Challenge
Explain what capped collections are in MongoDB and how you would use them to solve this problem. What are the key benefits of using capped collections?
A junior engineer might try to solve this problem by using a regular collection and then writing a separate script to periodically remove old logs. This would be a complex and inefficient solution.
A senior engineer would know that a capped collection is the perfect tool for this job. They would be able to explain what a capped collection is and how to use it to automatically remove old logs.
Step 1: Understand What Capped Collections Are
A capped collection is a fixed-size collection that automatically overwrites its oldest entries when it reaches its maximum size.
Step 2: The Key Features of Capped Collections
| Feature | Description |
|---|---|
| Size | Capped collections have a fixed size, which you specify when you create the collection. |
| Ordering | Capped collections maintain the insertion order of the documents. |
| TTL | Capped collections do not have a TTL (Time To Live) index. |
| Updates | You cannot update documents in a capped collection if the update would cause the document to grow in size. |
| Deletes | You cannot delete documents from a capped collection. |
Step 3: Use a Capped Collection
Here’s how we can create a capped collection to store our logs:
db.createCollection("my_logs", { capped: true, size: 100000 })In this example, we create a new capped collection called my_logs with a maximum size of 100,000 bytes.
The Benefits of Using Capped Collections
| Benefit | Description |
|---|---|
| Performance | Capped collections are very performant, because they are designed for high-throughput logging. |
| Simplicity | Capped collections are easy to use, and they automatically handle the removal of old logs. |
Practice Question
You are building a service that needs to store a log of the most recent user activity. Which of the following would be the most appropriate?