Docs Menu
Docs Home
/
MongoDB Manual
/ / /

Convert a Collection to Capped

On this page

  • About this Task
  • Before you Begin
  • Steps
  • Convert the collection to a capped collection
  • Confirm that the collection is capped
  • Learn More

To convert a non-capped collection to a capped collection, use the convertToCapped database command.

The convertToCapped command holds a database-exclusive lock for the duration of the operation. Other operations that lock the same database are blocked until the convertToCapped operation completes.

Generally, TTL (Time To Live) indexes offer better performance and more flexibility than capped collections. TTL indexes expire and remove data from normal collections based on the value of a date-typed field and a TTL value for the index.

Capped collections serialize inserts and therefore have worse concurrent insert performance than non-capped collections. Before you create a capped collection, consider if you can use a TTL index instead.

Create a non-capped collection called log2:

db.createCollection("log2")
1

To convert the log2 collection to a capped collection, run the convertToCapped command:

db.runCommand( {
convertToCapped: "log2",
size: 100000
} )

The log2 collection has a maximum size of 100,000 bytes.

2

To confirm that the log2 collection is now capped, use the isCapped() method:

db.log2.isCapped()
true

Back

Check if a Collection is Capped