定義
listCollectionsデータベース内のコレクションとビューの名前や作成オプションなどの情報を取得します。
listCollectionsコマンドは、データベース内のすべてのコレクションと ビュー のソートされていないリストを含むドキュメントを返します。返されたドキュメントを使用して、コレクションにカーソルを作成できます。mongoshprovides thedb.getCollectionInfos()and thedb.getCollectionNames()helper methods, as well as the show collections command.
互換性
このコマンドは、次の環境でホストされている配置で使用できます。
- MongoDB Atlas はクラウドでの MongoDB 配置のための完全管理サービスです
注意
このコマンドは、すべての MongoDB Atlas クラスターでサポートされています。すべてのコマンドに対する Atlas のサポートについては、「サポートされていないコマンド」を参照してください。
MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン
MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン
構文
このコマンドの構文は、次のとおりです。
db.runCommand( { listCollections: 1, filter: <document>, nameOnly: <boolean>, authorizedCollections: <boolean>, comment: <any> } )
コマンドフィールド
コマンドは以下の任意フィールドを取ることができます。
フィールド | タイプ | 説明 |
|---|---|---|
| ドキュメント | 任意。 コレクションの一覧をフィルター処理するクエリ述語。 You can specify a query predicate on any of the fields returned by |
| ブール値 | 任意。コマンドが名前とタイプ( デフォルト値は
|
| ブール値 | 任意。これはフラグであり、
デフォルト値は データベースに対する
|
| any | 任意。このコマンドに添付するユーザー指定のコメント。設定すると、このコメントは以下の場所にこのコマンドの記録と合わせて表示されます。
コメントには、有効な BSON 型(string, integer, object, array など)を使用できます。
|
動作
フィルター
Use a filter to limit the results of listCollections. You can specify a filter on any of the fields returned in the listCollections result set.
ロック
listCollections ロック動作:
MongoDB 5.0 より前のバージョンでは、
listCollectionsがデータベース上のインテント共有ロックを保持している場合、listCollectionsデータベース内の各コレクションのインテント共有ロックを引き受けます。MongoDB 5.0 以降では、
listCollectionsコレクションまたはデータベース上のインテント共有ロックを引き受けません。listCollectionsは、コレクションに対する排他書き込みロックを保持する操作によってブロックされません。
ロックについて、詳しくはFAQ: 同時実行を参照してください。
クライアントの切断
If the client that issued listCollections disconnects before the operation completes, MongoDB marks listCollections for termination using killOp.
レプリカセット ノードの状態の制限
To run on a replica set member, listCollections operations require the member to be in PRIMARY or SECONDARY state. If the member is in another state, such as STARTUP2, the operation errors.
必要なアクセス権
The listCollections command and its wrapper db.getCollectionInfos() require the listCollections action when access control is enforced. Users must have privileges that grant the listCollections action on the database to run 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
文字列
コレクション名。
タイプ
文字列
Type of data store. Returns
collectionfor collections,viewfor views, andtimeseriesfor time series collection.options
ドキュメント
コレクションオプション
これらのオプションは、
db.createCollection()で使用可能なオプションに直接対応します。オプションの説明については、db.createCollection()を参照してください。info
ドキュメント
コレクションに関連する次のフィールドを一覧表示します。
- readOnly
booleantrueの場合、データストアは読み取り専用となります。- uuid
- UUID一度確立されると、コレクションUUIDは変更されません。コレクション UUID は、シャーディングされたクラスター内のレプリカセット ノードでもシャードでも同一のままです。
idIndex
ドキュメント
コレクションの
_idインデックスに関する情報を提供します。
生のコマンド応答が必要ない場合は、db.getCollectionInfos() または db.getCollectionNames()ヘルパーメソッドを使用します。
例
すべてのコレクションを一覧表示
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 }
詳細
コレクションオプションの場合
コレクション情報の場合