AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

convertToCapped (データベースコマンド)

convertToCapped

警告

シャーディングされたコレクションではこのコマンドは実行しないでください

MongoDB does not support the convertToCapped command on sharded collections.

The convertToCapped command converts an existing, non-capped collection to a capped collection 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

変換する既存のコレクションの名前。

サイズ

上限付きコレクションの最大サイズ(バイト単位)。

writeConcern

任意。 コマンドの drop書込み保証( write concern ) を表すドキュメント。デフォルトの書込み保証を使用する場合は省略します。

comment

任意。このコマンドに添付するユーザー指定のコメント。設定すると、このコメントは以下の場所にこのコマンドの記録と合わせて表示されます。

コメントには、有効な BSON 型(string, integer, object, array など)を使用できます。

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 は元のコレクション内のドキュメントを自然な順序で走査し、ドキュメントを新しい Capped コレクションにロードします。

  • Capped コレクションに指定されたcapped sizeが元の Capped コレクションのサイズより小さい場合、MongoDB は挿入順序、または最初に追加された順序で、Capped コレクション内のドキュメントを上書きします。

  • 内部的には、コレクションを変換するために、MongoDB は次の手順を使用します

    • cloneCollectionAsCapped コマンドは、Cappedコレクションを作成し、データをインポートします。

    • MongoDB は元のコレクションを削除します。

    • renameCollection は、新しい Capped コレクションの名前を元のコレクションの名前に変更します。

  • これにより、操作中はデータベースの排他ロックが保持されます。同じデータベースをロックする他の操作は、操作が完了するまでブロックされます。データベースをロックする操作の場合、「一般的なクライアント操作によって取得されるロック」を参照してください。

警告

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.

次の例では、 db.collection.insertOne()を使用してeventsコレクションを作成し、 db.collection.stats()を使用してコレクションに関する情報を取得します。

db.events.insertOne( { click: 'button-1', time: new Date() } )
db.events.stats()

MongoDB は以下を返します。

{
"ns" : "test.events",
...
"capped" : false,
...
}

eventsコレクションを Capped コレクションに変換し、更新されたコレクション情報を表示するには、次のコマンドを実行します。

db.runCommand( { convertToCapped: 'events', size: 8192 } )
db.events.stats()

MongoDB は以下を返します。

{
"ns" : "test.events",
...
"capped" : true,
"max" : Long("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.

このページを評価