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

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

db.getCollectionNames()

Returns an array containing the names of all collections and views in the current database, or if running with access control, the names of the collections according to user's privilege. For details, see Required Access.

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

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud

注意

This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.

  • MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン

  • MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン

The db.getCollectionNames() is equivalent to:

db.runCommand(
{
listCollections: 1.0,
authorizedCollections: true,
nameOnly: true
}
)
  • 必要なアクセス権(データベースに対してlistCollectionsアクションを許可する特権)を持つユーザーの場合、このメソッドはデータベースのすべてのコレクションの名前を一覧表示します。

  • 必要なアクセス権を持たないユーザーの場合、このメソッドはユーザーが特権を持っているコレクションのみを一覧表示します。たとえば、ユーザーがデータベース内の特定のコレクションに対して find を持っている場合、メソッドはそのコレクションのみを返します。

If the client that issued db.getCollectionNames() disconnects before the operation completes, MongoDB marks db.getCollectionNames() for termination using killOp.

レプリカセット ノードでlistCollections操作を実行するには、ノードがPRIMARYまたはSECONDARY状態である必要があります。 ノードがSTARTUP2などの別の状態にある場合、操作はエラーになります。

以下は、 records データベース内のすべてのコレクションの名前を返します。

use records
db.getCollectionNames()

このメソッドは、コレクションの名前を以下のように配列で返します。

[ "employees", "products", "mylogs", "system.indexes" ]