Database Manual / Reference / Database Commands / Administration

cloneCollectionAsCapped (database command数据库命令)

Definition定义

cloneCollectionAsCapped
The cloneCollectionAsCapped command creates a new capped collection from an existing, non-capped collection within the same database. cloneCollectionAsCapped命令从同一数据库中的现有非封顶集合创建新的封顶集合The operation does not affect the original non-capped collection.该操作不会影响原始的无上限集合。

Compatibility兼容性

This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务

Important

This command is not supported in M0 and Flex clusters. For more information, see Unsupported Commands.M0和Flex集群不支持此命令。有关详细信息,请参阅不支持的命令

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Syntax语法

The command has the following syntax:该命令具有以下语法:

db.runCommand(
{
cloneCollectionAsCapped: <existing collection>,
toCollection: <capped collection>,
size: <capped size>,
writeConcern: <document>,
comment: <any>
}
)

Command Fields命令字段

The command takes the following fields:该命令包含以下字段:

Field字段Description描述
cloneCollectionAsCappedThe name of the existing collection to copy.要复制的现有集合的名称。
toCollection

The name of the new capped collection to create.要创建的新封顶集合的名称。

The name of the new capped collection must be distinct and cannot be the same as that of the original existing collection.新封顶集合的名称必须不同,不能与原始现有集合的名称相同。

sizeThe maximum size,in bytes, for the capped collection.封顶集合的最大大小(以字节为单位)。
·Optional. 可选。A document expressing the write concern of the drop command. Omit to use the default write concern.表示drop命令写入关注的文档。省略使用默认写入关注
comment

Optional. 可选。A user-provided comment to attach to this command. Once set, this comment appears alongside records of this command in the following locations:用户提供了要附加到此命令的注释。设置后,此注释将与此命令的记录一起出现在以下位置:

A comment can be any valid BSON type (string, integer, object, array, etc).注释可以是任何有效的BSON类型(字符串、整数、对象、数组等)。

The command copies an existing collection and creates a new capped collection with a maximum size specified by the capped size in bytes.该命令复制现有集合,并创建一个新的封顶集合,其最大大小由封顶大小(以字节为单位)指定。

To replace the original non-capped collection with a capped collection, use the convertToCapped command.要用封顶集合替换原始的非上限集合,请使用convertToCapped命令。

Behavior行为

If the capped size is less than the size of the source collection, then not all documents in the source collection will exist in the destination capped collection.如果封顶大小小于源集合的大小,则目标封顶集合中不会存在源集合中的所有文档。

This holds a database exclusive lock for the duration of the operation. Other operations which lock the same database will be blocked until the operation completes. See What locks are taken by some common client operations? for operations that lock the database.这将在操作期间保持数据库独占锁。锁定同一数据库的其他操作将被阻止,直到操作完成。请参阅一些常见的客户端操作会占用哪些锁?用于锁定数据库的操作。