convertToCapped

The command has the following syntax:

  1. { convertToCapped: <collection>, size: <capped size> , writeConcern: <document>}

The command takes the following fields:

FieldDescriptionconvertToCappedThe name of the existing collection to convert.sizeThe maximum size, in bytes, for the capped collection.writeConcernOptional. A document expressing the write concern of the drop command.Omit to use the default write concern.

convertToCapped takes an existing collection(<collection>) and transforms it into a capped collection witha maximum size in bytes, specified by the size argument(<capped size>).

During the conversion process, the convertToCappedcommand exhibits the following behavior:

  • MongoDB traverses the documents in the original collection innatural order and loads the documents into a newcapped collection.
  • If the capped size specified for the capped collection issmaller than the size of the original uncapped collection, thenMongoDB will overwrite documents in the capped collection basedon insertion order, or first in, first out order.
  • Internally, to convert the collection, MongoDB uses the followingprocedure
    • cloneCollectionAsCapped command creates the cappedcollection and imports the data.
    • MongoDB drops the original collection.
    • renameCollection renames the new capped collectionto the name of the original collection.
  • This holds a database exclusive lock for the duration of the operation.Other operations which lock the same database will be blocked until theoperation completes. See What locks are taken by some common client operations? foroperations that lock the database.

Note

MongoDB does not support the convertToCappedcommand in a sharded cluster.

Warning

The convertToCapped will not recreate indexes fromthe original collection on the new collection, other than theindex on the _id field. If you need indexes on thiscollection you will need to create these indexes after theconversion is complete.

Example

Convert a Collection

The following example uses a db.collection.save() operation to createan events collection, and db.collection.stats() to obtaininformation about the collection:

  1. db.events.save( { click: 'button-1', time: new Date() } )
  2. db.events.stats()

MongoDB will return the following:

  1. {
  2. "ns" : "test.events",
  3. ...
  4. "capped" : false,
  5. ...
  6. }

To convert the events collection into a capped collection and view theupdated collection information, run the following commands:

  1. db.runCommand( { convertToCapped: 'events', size: 8192 } )
  2. db.events.stats()

MongoDB will return the following:

  1. {
  2. "ns" : "test.events",
  3. ...
  4. "capped" : true,
  5. "max" : NumberLong("9223372036854775807"),
  6. "maxSize" : 8192,
  7. ...
  8. }

The convertToCapped will not recreate indexes fromthe original collection on the new collection, other than theindex on the _id field. If you need indexes on thiscollection you will need to create these indexes after theconversion is complete.

See also

create