convertToCapped
convertToCapped
- The
convertToCapped
command converts an existing,non-capped collection to a capped collection within the samedatabase.
The command has the following syntax:
- { 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 convertToCapped
command 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 convertToCapped
command 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:
- db.events.save( { click: 'button-1', time: new Date() } )
- db.events.stats()
MongoDB will return the following:
- {
- "ns" : "test.events",
- ...
- "capped" : false,
- ...
- }
To convert the events
collection into a capped collection and view theupdated collection information, run the following commands:
- db.runCommand( { convertToCapped: 'events', size: 8192 } )
- db.events.stats()
MongoDB will return the following:
- {
- "ns" : "test.events",
- ...
- "capped" : true,
- "max" : NumberLong("9223372036854775807"),
- "maxSize" : 8192,
- ...
- }
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