定義
db.getCollectionInfos(filter, options)現在のデータベースのコレクションまたは ビューの情報(名前やオプションなど)を含むドキュメントの配列を返します。結果は、ユーザーの特権によって異なります。詳細については、 必要なアクセス権 を参照してください。
db.getCollectionInfos()ヘルパーはlistCollectionsコマンドをラップします。
構文
db.getCollectionInfos() メソッドには次のパラメーターがあります。
Parameter | タイプ | 説明 |
|---|---|---|
| ドキュメント | |
| ドキュメント | 任意。コマンドの追加オプションを指定するドキュメント。使用可能なオプションの一覧については、「 オプション 」を参照してください。 |
オプション
db.getCollectionInfos() コマンドの optionsドキュメントで次のオプションを指定できます。
オプション | タイプ | 説明 |
|---|---|---|
| ブール値 | 任意。コマンドが名前とタイプ( デフォルト値は
の例については、「 コレクション名のみを返す 」を参照してください。 |
| ブール値 | 任意。
デフォルト値は データベースに対する
|
互換性
このメソッドは、次の環境でホストされている配置で使用できます。
MongoDB Atlas はクラウドでの MongoDB 配置のためのフルマネージド サービスです
注意
このコマンドは、すべての MongoDB Atlas クラスターでサポートされています。すべてのコマンドに対する Atlas のサポートについては、「サポートされていないコマンド」を参照してください。
MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン
MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン
必要なアクセス権
アクセス制御が強制されている場合、listCollections コマンドとそのラッパーdb.getCollectionInfos() では、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 } )
show collections
mongosh メソッドの show collections は、次のようになります。
db.runCommand( { listCollections: 1.0, authorizedCollections: true, nameOnly: true } )
必要なアクセス権を持つユーザーの場合、
show collectionsではデータベースの非システム コレクションが一覧表示されます。必要なアクセス権を持たないユーザーの場合、
show collectionsではユーザーに特権があるコレクションのみが一覧表示されます。
動作
クライアントの切断
操作が完了する前にdb.getCollectionInfos()を発行したクライアントが切断された場合、MongoDB はdb.getCollectionInfos()をkillOpを使用して終了対象としてマークします。
レプリカセット ノードの状態の制限
レプリカセット ノードでlistCollections操作を実行するには、ノードがPRIMARYまたはSECONDARY状態である必要があります。 ノードがSTARTUP2などの別の状態にある場合、操作はエラーになります。
例
データベース内のすべてのコレクションの情報を返す
以下は、example データベース内の全コレクションの情報を返します。
use example db.getCollectionInfos()
[ { "name" : "employees", "type" : "collection", "options" : { "flags" : 1, "validator" : { "$or" : [ { "phone" : { "$exists" : true } }, { "email" : { "$exists" : true } } ] } }, "info" : { "readOnly" : false, "uuid" : UUID("222e18ca-4a10-4a42-a8fe-c39255cc4c55") }, "idIndex" : { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "example.employees" } }, { "name" : "products", "type" : "collection", "options" : { "flags" : 1 }, "info" : { "readOnly" : false, "uuid" : UUID("1bc898b2-3b91-45e4-9d8b-0be462d5a157") }, "idIndex" : { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "example.products" } }, { "name" : "mylogs", "type" : "collection", "options" : { "capped" : true, "size" : 256 }, "info" : { "readOnly" : true, "uuid" : UUID("8e62116d-b6a0-490a-808c-258ccb7ea947") }, "idIndex" : { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "example.mylogs" } } ]
コレクション名のみを返します
現在のデータベース内のコレクションの名前のみを返すには、nameOnly: true オプションを指定します。 (例: )。
use example db.getCollectionInfos( {}, { nameOnly: true } )
[ { name: 'products', type: 'collection' }, { name: 'weather', type: 'timeseries' }, { name: 'system.buckets.weather', type: 'collection' }, { name: 'system.views', type: 'collection' }, { name: 'sales', type: 'collection' } ]
特定のコレクションの情報を返す
特定のコレクションのコレクション情報をリクエストには、フィルタードキュメントでコレクション名を指定します。次の例では、exampleデータベース内の employeesコレクションのコレクション情報を詳述する 1 つのドキュメントを含む配列が返されます。
use example db.getCollectionInfos( { name: "employees" } )
[ { "name" : "employees", "type" : "collection", "options" : { "flags" : 1, "validator" : { "$or" : [ { "phone" : { "$exists" : true } }, { "email" : { "$exists" : true } } ] } }, "info" : { "readOnly" : false, "uuid" : UUID("222e18ca-4a10-4a42-a8fe-c39255cc4c55") }, "idIndex" : { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "example.employees" } } ]
読み取り専用コレクションの情報を返す
db.getCollectionInfos() によって返されるフィールドのいずれかにフィルタを指定できます。
たとえば、次のコマンドは、example データベース内で info.readOnly が true であるすべてのコレクションの情報を返します。
use example db.getCollectionInfos( { "info.readOnly" : true } )
[ { "name" : "mylogs", "type" : "collection", "options" : { "capped" : true, "size" : 256 }, "info" : { "readOnly" : true, "uuid" : UUID("8e62116d-b6a0-490a-808c-258ccb7ea947") }, "idIndex" : { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "example.mylogs" } } ]