定義
db.getCollectionInfos(filter, options)現在のデータベースのコレクションまたは ビュー情報 (名前やオプションなど) を含むドキュメントの配列を返します。結果は、ユーザーの権限によって異なります。詳細については、 必要なアクセス権 を参照してください。
db.getCollectionInfos()ヘルパーはlistCollectionsコマンドをラップします。
構文
db.getCollectionInfos() メソッドには次のパラメーターがあります。
Parameter | タイプ | 説明 |
|---|---|---|
| ドキュメント | 任意。 コレクションの一覧をフィルター処理するクエリ述語。
|
| ドキュメント | Optional. A document that specifies additional options for the command. For a list of available options, see Options. |
オプション
db.getCollectionInfos() コマンドの optionsドキュメントで次のオプションを指定できます。
オプション | タイプ | 説明 |
|---|---|---|
| ブール値 | 任意。コマンドが名前とタイプ( デフォルト値は
For an example, see Return Only Collection Names. |
| ブール値 | 任意。
デフォルト値は データベースに対する
|
互換性
このメソッドは、次の環境でホストされている配置で使用できます。
- MongoDB Atlas はクラウドでの MongoDB 配置のための完全管理サービスです
注意
このコマンドは、すべての MongoDB Atlas クラスターでサポートされています。すべてのコマンドに対する Atlas のサポートについては、「サポートされていないコマンド」を参照してください。
MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン
MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン
必要なアクセス権
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.
For example, the following command grants the privilege to run db.getCollectionInfos() against the test database:
{ 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ではユーザーに特権があるコレクションのみが一覧表示されます。
動作
クライアントの切断
If the client that issued db.getCollectionInfos() disconnects before the operation completes, MongoDB marks db.getCollectionInfos() for termination using 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" } } ]