On this page本页内容
convertToCapped
MongoDB does not support the MongoDB在分片集群中不支持convertToCapped
command in a sharded cluster.convertToCapped
命令。
The convertToCapped
command converts an existing, non-capped collection to a capped collection within the same database.convertToCapped
命令将同一数据库中的现有非封顶集合转换为封顶集合。
The command has the following syntax:该命令具有以下语法:
{ convertToCapped: <collection>,
size: <capped size>,
writeConcern: <document>,
comment: <any>
}
The command takes the following fields:该命令接受以下字段:
convertToCapped | |
size | |
writeConcern | drop command. drop 命令的写入关注的文档。 |
comment |
|
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>
).convertToCapped
获取一个现有集合(<collection>
),并将其转换为一个有上限的集合,该集合的最大大小由size
参数(<caped size>
)指定,以字节为单位。
During the conversion process, the 在转换过程中,convertToCapped
command exhibits the following behavior:convertToCapped
命令显示以下行为:
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.capped size
小于原始未封顶集合的大小,则MongoDB将根据插入顺序或先入先出顺序覆盖封顶集合中的文档。Internally, to convert the collection, MongoDB uses the following procedure在内部,为了转换集合,MongoDB使用以下过程
cloneCollectionAsCapped
renameCollection
The convertToCapped
will not recreate indexes from the original collection on the new collection, other than the index on the 将不会在新集合上从原始集合重新创建索引,_id
field. _id
字段上的索引除外。If you need indexes on this collection you will need to create these indexes after the conversion is complete.如果需要此集合上的索引,则需要在转换完成后创建这些索引。
The following example uses 以下示例使用db.collection.insertOne()
to create an events
collection, and db.collection.stats()
to obtain information about the collection:db.collection.insertOne()
创建events
集合,使用db.collection.stats()
获取有关集合的信息:
db.events.insertOne( { click: 'button-1', time: new Date() } ) db.events.stats()
MongoDB will return the following:MongoDB将返回以下内容:
{ "ns" : "test.events", ... "capped" : false, ... }
To convert the 要将events
collection into a capped collection and view the updated collection information, run the following commands:events
集合转换为封顶集合并查看更新的集合信息,请运行以下命令:
db.runCommand( { convertToCapped: 'events', size: 8192 } ) db.events.stats()
MongoDB will return the following:MongoDB将返回以下内容:
{ "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. convertToCapped
不会在新集合上从原始集合重新创建索引,而不是_id
字段上的索引。If you need indexes on this collection you will need to create these indexes after the conversion is complete.如果需要此集合上的索引,则需要在转换完成后创建这些索引。