AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

listCollections(データベースコマンド)

listCollections

データベース内のコレクションとビューの名前や作成オプションなどの情報を取得します。

listCollections コマンドは、データベース内のすべてのコレクションと ビュー のソートされていないリストを含むドキュメントを返します。返されたドキュメントを使用して、コレクションにカーソルを作成できます。

mongosh provides the db.getCollectionInfos() and the db.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>
}
)

コマンドは以下の任意フィールドを取ることができます。

フィールド
タイプ
説明

filter

ドキュメント

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

You can specify a query predicate on any of the fields returned by listCollections.

nameOnly

ブール値

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

デフォルト値は false です。

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

authorizedCollections

ブール値

任意。これはフラグであり、true に設定して nameOnly: true と合わせて使用すると、アクセスコントロールが適用されている場合に、必要な特権を持たないユーザーにも(データベース上での listCollections アクション)コマンドの実行を許可します。

authorizedCollections オプションと nameOnly オプションの両方が true に設定されている場合、コマンドを実行するとユーザーが特権を持つコレクションのみが返されます。たとえば、ユーザーが特定のコレクションに対して find アクションの特権を有する場合、コマンドではそれらのコレクションのみが返されます。また、ユーザーがデータベースリソースに対して find やその他のアクションの特権を有する場合、コマンドではデータベース内のすべてのコレクションが一覧表示されます。

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

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

nameOnly: true なしで使用しても、このオプションには効果がありません。つまり、この場合にアクセスコントロールが適用されていれば、ユーザーはコマンドを実行するために必要な特権を持っている必要があります。特権がない限り、ユーザーがコマンドを実行することはできません。

comment

any

任意。このコマンドに添付するユーザー指定のコメント。設定すると、このコメントは以下の場所にこのコマンドの記録と合わせて表示されます。

コメントには、有効な BSON 型(string, integer, object, array など)を使用できます。

listCollections コマンドに設定されたコメントは、listCollections カーソルで実行される後続の getMore コマンドに継承されます。

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 特権を持たないユーザーは、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
}
)
listCollections.cursor

コレクション名とオプションを含むドキュメントへのカーソルを作成するために必要な情報を含むドキュメント。カーソル情報には、カーソルID、コマンドの名前空間全体、および結果の最初のバッチが含まれます。バッチする出力内の各ドキュメントには以下のフィールドが含まれます。

フィールド
タイプ
説明

name

文字列

コレクション名。

タイプ

文字列

Type of data store. Returns collection for collections, view for views, and timeseries for time series collection.

options

ドキュメント

コレクションオプション

これらのオプションは、 db.createCollection()で使用可能なオプションに直接対応します。オプションの説明については、 db.createCollection()を参照してください。

info

ドキュメント

コレクションに関連する次のフィールドを一覧表示します。

readOnly
booleantrueの場合、データストアは読み取り専用となります。
uuid
UUID一度確立されると、コレクションUUIDは変更されません。コレクション UUID は、シャーディングされたクラスター内のレプリカセット ノードでもシャードでも同一のままです。

idIndex

ドキュメント

コレクションの_idインデックスに関する情報を提供します。

listCollections.ok

コマンドの戻り値です。値が1の場合は成功を示します。

生のコマンド応答が必要ない場合は、db.getCollectionInfos() または db.getCollectionNames()ヘルパーメソッドを使用します。

musicデータベースには、3 つのコレクションmotorheadtaylorSwift 、および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
}

コレクションオプションの場合

コレクション情報の場合