On this page本页内容
convertToCapped
¶Do Not Run This Command In Sharded Clusters
MongoDB does not support the convertToCapped
command in a sharded cluster.
The convertToCapped
command converts an existing, non-capped collection to a capped collection within the same database.
The command has the following syntax:语法如下所示:
The command takes the following fields:
convertToCapped | The name of the existing collection to convert. |
size | The maximum size, in bytes, for the capped collection. |
writeConcern | drop command. Omit to use the default write concern. |
comment |
A comment can be any valid BSON type (string, integer, object, array, etc).
|
convertToCapped
takes an existing collection (<collection>
) and transforms it into a capped collection with a maximum size in bytes, specified by the size
argument (<capped size>
).
During the conversion process, the convertToCapped
command exhibits the following behavior:
capped size
specified for the capped collection is smaller than the size of the original uncapped collection, then MongoDB will overwrite documents in the capped collection based on insertion order, or first in, first out order.cloneCollectionAsCapped
command creates the capped collection and imports the data.renameCollection
renames the new capped collection to the name of the original collection.Warning
The convertToCapped
will not recreate indexes from the original collection on the new collection, other than the index on the _id
field. If you need indexes on this collection you will need to create these indexes after the conversion is complete.
The following example uses a db.collection.save()
operation to create an events
collection, and db.collection.stats()
to obtain information about the collection:
MongoDB will return the following:
To convert the events
collection into a capped collection and view the updated collection information, run the following commands:
MongoDB will return the following:
The convertToCapped
will not recreate indexes from the original collection on the new collection, other than the index on the _id
field. If you need indexes on this collection you will need to create these indexes after the conversion is complete.
See also参阅