对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

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.

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