convertToCapped
定义
convertToCapped
警告
Do Not Run This Command On Sharded Collections
MongoDB does not support the
convertToCapped
command on sharded collections.The
convertToCapped
command converts an existing, non-capped collection to a 固定集合 within the same database.
兼容性
此命令可用于以下环境中托管的部署:
MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务
重要
无服务器实例不支持此命令。 有关更多信息,请参阅不支持的命令。
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
语法
该命令具有以下语法:
db.runCommand( { convertToCapped: <collection>, size: <capped size>, writeConcern: <document>, comment: <any> } )
命令字段
该命令接受以下字段:
字段 | 说明 |
---|---|
convertToCapped | The name of the existing collection to convert. |
size | The maximum size, in bytes, for the capped collection. |
writeConcern | 可选。 表达 命令 写关注(write concern) |
| 可选。用户提供的待附加到该命令的注释。设置后,该注释将与该命令的记录一起出现在以下位置:
注释可以是任何有效的 BSON 类型(字符串、整型、对象、数组等)。 |
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:
MongoDB traverses the documents in the original collection in 自然顺序 and loads the documents into a new capped collection.
If the
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.Internally, to convert the collection, MongoDB uses the following procedure
cloneCollectionAsCapped
command creates the capped collection and imports the data.MongoDB drops the original collection.
renameCollection
renames the new capped collection to the name of the original collection.
此功能会在操作期间保持数据库独占锁。锁定同一数据库的其他操作将被阻止,直到该操作完成。请参阅某些常见客户端操作会采用哪些锁?锁定数据库的操作。
警告
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.
例子
Convert a Collection
The following example uses db.collection.insertOne()
to create
an events
collection, and db.collection.stats()
to obtain
information about the collection:
db.events.insertOne( { 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 the
updated 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 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.