定義
db.getCollectionInfos(filter, nameOnly, authorizedCollections)現在のデータベースのコレクションまたは ビューの情報(名前やオプションなど)を含むドキュメントの配列を返します。結果は、ユーザーの特権によって異なります。 詳細については、 「必要なアクセス権」 を参照してください。
db.getCollectionInfos()ヘルパーはlistCollectionsコマンドをラップします。db.getCollectionInfos()メソッドには次の任意 パラメーターがあります。Parameterタイプ説明filterドキュメント
任意。コレクションの一覧をフィルター処理するクエリ式です。
nameOnlyブール値
任意。 コマンドが名前とタイプのみを返すか(
view、collection、またはtimeseries)を返すか、名前とその他の情報の両方を返すかを示すフラグです。デフォルト値は
falseです。nameOnlyがtrueの場合、filter式はコレクションの名前と型に基づいてのみフィルタリングできます。他のフィールドは使用できません。authorizedCollectionsブール値
任意。これはフラグであり、
trueに設定してnameOnly: trueと合わせて使用すると、アクセスコントロールが適用されている場合に、必要な特権を持たないユーザーにも(データベース上でのlistCollectionsアクション)コマンドの実行を許可します。authorizedCollectionsオプションとnameOnlyオプションの両方が true に設定されている場合、コマンドを実行するとユーザーが特権を持つコレクションのみが返されます。たとえば、ユーザーが特定のコレクションに対してfindアクションの特権を有する場合、コマンドではそれらのコレクションのみが返されます。また、ユーザーがデータベースリソースに対してfindやその他のアクションの特権を有する場合、コマンドではデータベース内のすべてのコレクションが一覧表示されます。デフォルト値は
falseです。つまり、コマンドを実行するには、ユーザがデータベースに対してlistCollectionsアクションを持っている必要があります。データベースに対する
listCollectionsアクションの特権を持つユーザーの場合、データベース内のコレクションを一覧表示する特権がすでにあるため、このオプションは効果がありません。nameOnly: trueなしで使用しても、このオプションには効果がありません。つまり、この場合にアクセスコントロールが適用されていれば、ユーザーはコマンドを実行するために必要な特権を持っている必要があります。特権がない限り、ユーザーがコマンドを実行することはできません。
互換性
このメソッドは、次の環境でホストされている配置で使用できます。
MongoDB Atlas はクラウドでの MongoDB 配置のためのフルマネージド サービスです
注意
このコマンドは、すべての MongoDB Atlas クラスターでサポートされています。すべてのコマンドに対する Atlas のサポートについては、「サポートされていないコマンド」を参照してください。
MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン
MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン
必要なアクセス権
db.getCollectionInfos()はlistCollectionsのラッパーであるため、アクセス制御が強制されている場合、ユーザーはlistCollectionsと同じ特権を持っている必要があります。
アクセス制御が強制されている場合、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 } )
show collections
mongosh メソッドの show collections は、次のようになります。
db.runCommand( { listCollections: 1.0, authorizedCollections: true, nameOnly: true } )
必要なアクセス権を持つユーザーの場合、
show collectionsではデータベースの非システム コレクションが一覧表示されます。必要なアクセス権を持たないユーザーの場合、
show collectionsではユーザーに特権があるコレクションのみが一覧表示されます。
動作
クライアントの切断
MongoDB 4.2以降では、 db.getCollectionInfos()を発行したクライアントが操作の完了前に切断した場合、MongoDB は killOp を使用してdb.getCollectionInfos()を終了対象としてマークし 。
レプリカセット ノードの状態の制限
レプリカセット ノードで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" } } ]
特定のコレクションのコレクション情報をリクエストするには、次のように、メソッドを呼び出すときにコレクション名を指定します。
use example db.getCollectionInfos( { name: "employees" } )
このメソッドはexample データベースの employees コレクションのコレクション情報を詳述する 1 つのドキュメントを含む配列を返します。
[ { "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" } } ]