Docs Menu
Docs Home
/ /

db.getCollectionInfos()(mongoshメソッド)

db.getCollectionInfos(filter, options)

現在のデータベースのコレクションまたは ビューの情報(名前やオプションなど)を含むドキュメントの配列を返します。結果は、ユーザーの特権によって異なります。詳細については、 必要なアクセス権 を参照してください。

db.getCollectionInfos()ヘルパーはlistCollections コマンドをラップします。

db.getCollectionInfos() メソッドには次のパラメーターがあります。

Parameter
タイプ
説明

filter

ドキュメント

任意。 コレクションの一覧をフィルター処理するクエリ述語。

db.getCollectionInfos()によって返されるフィールドのいずれかにクエリ述語を指定できます。

options

ドキュメント

任意。コマンドの追加オプションを指定するドキュメント。使用可能なオプションの一覧については、「 オプション 」を参照してください。

db.getCollectionInfos() コマンドの optionsドキュメントで次のオプションを指定できます。

オプション
タイプ
説明

nameOnly

ブール値

任意。コマンドが名前とタイプ(viewcollection、または timeseries)のみを返すか、名前とその他の情報の両方を返すかを示すフラグです。

デフォルト値は false です。

nameOnlytrue の場合、filter 式はコレクションの名前と型に基づいてのみフィルタリングできます。他のフィールドは使用できません。

の例については、「 コレクション名のみを返す 」を参照してください。

authorizedCollections

ブール値

任意。true nameOnly: trueに設定し、 と併用すると、アクセス制御が強制されている場合に、必要な特権(データベースのlistCollections アクションなど)を持たないユーザーにもdb.getCollectionInfos() の実行を許可します。

authorizedCollections オプションと nameOnly オプションの両方が true に設定されている場合、コマンドを実行するとユーザーが特権を持つコレクションのみが返されます。以下に例を挙げます。

  • findユーザーが特定のコレクションに対して アクションを持っている場合、コマンドはそれらのコレクションのみを返します。

  • ユーザーがデータベースリソースに対して任意の権限アクションを持っている場合、コマンドはデータベース内のすべてのコレクションを一覧表示します。

デフォルト値はfalse listCollectionsであり、コマンドを実行するにはユーザーがデータベースに対して アクションを持っている必要があります。

データベースに対する listCollections アクションの特権を持つユーザーの場合、データベース内のコレクションを一覧表示する特権がすでにあるため、このオプションは効果がありません。

nameOnly: true なしで使用しても、このオプションには効果がありません。

このメソッドは、次の環境でホストされている配置で使用できます。

  • 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 特権を持たないユーザーは、authorizedCollectionsnameOnly の両方が trueに設定されている場合に、listCollections を実行できます。この場合、コマンドではユーザーが特権を持つコレクションの名前と型が返されます。

以下のように find 特権のあるロールのユーザーを例に取ります。

{ resource: { db: "sales", collection: "currentQuarter" }, actions: [ "find" ] }

authorizedCollectionsnameOnly の両方が 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
}
)

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.readOnlytrue であるすべてのコレクションの情報を返します。

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"
}
}
]

戻る

db.getCollection

項目一覧