定義
listCollections
データベース内のコレクションとビューの名前や作成オプションなどの情報を取得します。
listCollections
コマンドは、コレクションにカーソルを作成する際に使用できる情報を含むドキュメントを返します。mongosh
は、db.getCollectionInfos()
およびdb.getCollectionNames()
ヘルパーメソッド、および show collections コマンドを提供します。
互換性
このコマンドは、次の環境でホストされている配置で使用できます。
MongoDB Atlas はクラウドでの MongoDB 配置のためのフルマネージド サービスです
注意
このコマンドは、すべての MongoDB Atlas クラスターでサポートされています。すべてのコマンドに対する Atlas のサポートについては、「サポートされていないコマンド」を参照してください。
MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン
MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン
構文
このコマンドの構文は、次のとおりです。
db.runCommand( { listCollections: 1, filter: <document>, nameOnly: <boolean>, authorizedCollections: <boolean>, comment: <any> } )
コマンドフィールド
コマンドは以下の任意フィールドを取ることができます。
フィールド | タイプ | 説明 |
---|---|---|
| ドキュメント | 任意。コレクションの一覧をフィルター処理するクエリ式です。 |
| ブール値 | 任意。 コマンドが名前とタイプのみを返すか( デフォルト値は
|
| ブール値 | 任意。これはフラグであり、
デフォルト値は データベースに対する
|
| any | 任意。このコマンドに添付するユーザー指定のコメント。設定すると、このコメントは以下の場所にこのコマンドの記録と合わせて表示されます。
コメントには、有効な BSON 型(string, integer, object, array など)を使用できます。
|
動作
フィルター
フィルターを使用して、 listCollections
の結果を制限します。 の結果セットでfilter
返されるフィールド のいずれかに listCollections
を指定できます。
ロック
listCollections
ロック動作:
MongoDB 5.0 より前のバージョンでは、
listCollections
がデータベース上のインテント共有ロックを保持している場合、listCollections
データベース内の各コレクションのインテント共有ロックを引き受けます。MongoDB 5.0 以降では、
listCollections
コレクションまたはデータベース上のインテント共有ロックを引き受けません。listCollections
は、コレクションに対する排他書き込みロックを保持する操作によってブロックされません。
ロックの詳細については、「 MongoDB ではどのようなタイプのロックを使用しますか 」を参照してください。
クライアントの切断
操作が完了する前にlistCollections
を発行したクライアントが切断された場合、MongoDB はlistCollections
をkillOp
を使用して終了対象としてマークします。
レプリカセット ノードの状態の制限
レプリカセット ノード上で listCollections
操作を実行するには、ノードが PRIMARY
または SECONDARY
状態である必要があります。ノードが STARTUP2
などの別の状態にある場合、操作はエラーになります。
必要なアクセス権
アクセス制御が強制されている場合、 listCollections
コマンドにはlistCollections
アクションが必要です。 listCollections
を実行するには、ユーザーにはデータベースに対するlistCollections
アクションを許可する特権が必要です。
たとえば、次のコマンドは、 test
データベースに対してdb.getCollectionInfos()
を実行する特権を付与します。
{ resource: { db: "test", collection: "" }, actions: [ "listCollections" ] }
組み込みロール read
には、特定のデータベースに対してlistCollections
を実行する特権が付与されます。
必要な read
特権を持たないユーザーは、authorizedCollections
と nameOnly
の両方が true
に設定されている場合に、listCollections
を実行できます。この場合、コマンドではユーザーが特権を持つコレクションの名前と型が返されます。
以下のように find
特権のあるロールのユーザーを例に取ります。
{ resource: { db: "sales", collection: "currentQuarter" }, actions: [ "find" ] }
authorizedCollections
と nameOnly
の両方が true
に設定されている場合に、このユーザーは listCollections
を実行できます。
db.runCommand( { listCollections: 1.0, authorizedCollections: true, nameOnly: true } )
この操作は、 currentQuarter
コレクションの名前と種類を返します。
ただし、ユーザーに必要なアクセス権の承認がない場合、次の操作ではエラーが返されます。
db.runCommand( { listCollections: 1.0, authorizedCollections: true } ) db.runCommand( { listCollections: 1.0, nameOnly: true } )
出力
listCollections.cursor
コレクション名とオプションを含むドキュメントへのカーソルを作成するための情報を含むドキュメント。カーソル情報には、カーソルID、コマンドの名前空間全体、および結果の最初のバッチが含まれます。バッチ出力内の各ドキュメントには以下のフィールドが含まれます。
フィールドタイプ説明name
文字列
コレクション名。
タイプ
文字列
options
ドキュメント
コレクションオプション
これらのオプションは、
db.createCollection()
で使用可能なオプションに直接対応します。オプションの説明については、db.createCollection()
を参照してください。info
ドキュメント
コレクションに関連する次のフィールドを一覧表示します。
- readOnly
boolean
true
の場合、データストアは読み取り専用となります。- uuid
- UUID一度確立されると、コレクションUUIDは変更されません。コレクションUUIDは、シャーディングされたクラスター内のレプリカセットノードでもシャードでも同一のままになります。
idIndex
ドキュメント
コレクションの
_id
インデックスに関する情報を提供します。
例
すべてのコレクションを一覧表示
music
データベースには、3 つのコレクションmotorhead
、 taylorSwift
、およびramones
が含まれています。
コレクション名のリストを取得するには、nameOnly
オプションを指定して listCollections
コマンドを実行します。
db.runCommand( { listCollections: 1.0, nameOnly: true } )
出力は次のとおりです。
{ cursor: { id: Long("0"), ns: 'music.$cmd.listCollections', firstBatch: [ { name: 'motorhead', type: 'collection' }, { name: 'taylorSwift', type: 'collection' }, { name: 'ramones', type: 'collection' } ] }, ok: 1 }
より詳細な情報を取得するには、 nameOnly
オプションを削除してください。
db.runCommand( { listCollections: 1.0 } )
出力は次のとおりです。
{ cursor: { id: Long("0"), ns: 'music.$cmd.listCollections', firstBatch: [ { name: 'motorhead', type: 'collection', options: {}, info: { readOnly: false, uuid: new UUID("09ef1858-2831-47d2-a3a7-9a29a9cfeb94") }, idIndex: { v: 2, key: { _id: 1 }, name: '_id_' } }, { name: 'taylorSwift', type: 'collection', options: {}, info: { readOnly: false, uuid: new UUID("6c46c8b9-4999-4213-bcef-9a36b0cff228") }, idIndex: { v: 2, key: { _id: 1 }, name: '_id_' } }, { name: 'ramones', type: 'collection', options: {}, info: { readOnly: false, uuid: new UUID("7e1925ba-f2f9-4e42-90e4-8cafd434a6c4") }, idIndex: { v: 2, key: { _id: 1 }, name: '_id_' } } ] }, ok: 1 }
詳細
コレクションオプションの場合
コレクション情報の場合