Skip to content

MongoDB Capped Collections

In MongoDB, a capped collection is a special type of collection that has a fixed size and circular writing characteristics. Capped collections can be used to store log data, event data, and other data that needs to be stored and read in chronological order.

Basic Concepts

How Capped Collections Work

The size of a capped collection is fixed. When the size of the collection reaches the maximum value, MongoDB automatically deletes the oldest documents to make space for new documents. The writing operations of capped collections are circular, so they are suitable for storing data that needs to be stored and read in chronological order.

Characteristics of Capped Collections

  1. Fixed Size: The size of a capped collection is fixed.
  2. Circular Writing: When the size of the collection reaches the maximum value, MongoDB automatically deletes the oldest documents.
  3. Sequential Reading: The documents in a capped collection are stored in the order of insertion, so reading operations are sequential.
  4. High Performance: Writing and reading operations of capped collections are high-performance because they do not need to maintain indexes.

Creating Capped Collections

Basic Syntax

javascript
// Create a capped collection
db.createCollection("mycappedcollection", {
  capped: true,
  size: 1000000, // Size of the collection in bytes
  max: 1000 // Maximum number of documents in the collection
})

Example

javascript
// Create a capped collection with a size of 1MB and a maximum of 1000 documents
db.createCollection("logs", {
  capped: true,
  size: 1024 * 1024,
  max: 1000
})

Querying Capped Collections

Querying All Documents

javascript
// Query all documents
db.logs.find()

Querying the Last Inserted Documents

javascript
// Query the last 10 inserted documents
db.logs.find().sort({ $natural: -1 }).limit(10)

Querying the First Inserted Documents

javascript
// Query the first 10 inserted documents
db.logs.find().sort({ $natural: 1 }).limit(10)

Modifying Capped Collections

Modifying a Document

javascript
// Modify a document
db.logs.updateOne(
  { _id: ObjectId("5e8f8f8f8f8f8f8f8f8f8f8f") },
  { $set: { level: "error" } }
)

Deleting a Document

javascript
// Delete a document
db.logs.deleteOne({ _id: ObjectId("5e8f8f8f8f8f8f8f8f8f8f8f") })

Best Practices for Capped Collections

Choose the Right Size

We should choose the appropriate size of the capped collection based on the size of the data and the retention time. If the size of the capped collection is too small, data will be overwritten quickly; if the size of the capped collection is too large, storage space will be wasted.

Avoid Frequent Queries

We should avoid frequent queries on capped collections because query operations will affect the performance of writing operations.

Regular Cleaning

We can regularly clean up expired data in capped collections to ensure that the size of the capped collection does not exceed the limit.

Summary

In MongoDB, a capped collection is a special type of collection that has a fixed size and circular writing characteristics. Capped collections are suitable for storing data that needs to be stored and read in chronological order, such as log data and event data. The writing and reading operations of capped collections are high-performance because they do not need to maintain indexes. When using capped collections, we should choose the appropriate size, avoid frequent queries, and clean up regularly to ensure the efficient operation of the capped collection.

Content is for learning and research only.