Database Manual / Reference / mongosh Methods / Databases

db.getCollectionNames() (mongosh method方法)

Definition定义

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.有关详细信息,请参阅所需访问权限

Compatibility兼容性

This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务

Note

This command is supported in all MongoDB Atlas clusters. 所有MongoDB Atlas集群都支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Required Access所需访问权限

The db.getCollectionNames() is equivalent to:db.getCollectionNames()等效于:

db.runCommand(
{
listCollections: 1.0,
authorizedCollections: true,
nameOnly: true
}
)
  • For users with the required access (privileges that grant listCollections action on the database), the method lists the names of all collections for the database.对于具有所需访问权限(授予数据库listCollections操作的权限)的用户,该方法列出了数据库的所有集合的名称。
  • For users without the required access, the method lists only the collections for which the users has privileges. For example, if a user has find on a specific collection in a database, the method would return just that collection.对于没有所需访问权限的用户,该方法仅列出用户具有权限的集合。例如,如果用户在数据库中的特定集合上进行了find,则该方法将仅返回该集合。

Behavior行为

Client Disconnection客户端断开连接

If the client that issued db.getCollectionNames() disconnects before the operation completes, MongoDB marks db.getCollectionNames() for termination using killOp.如果发出db.getCollectionNames()的客户端在操作完成之前断开连接,MongoDB将使用killOp标记db.getCollectionNames()以终止。

Replica Set Member State Restriction副本集成员国限制

To run on a replica set member, listCollections operations require the member to be in PRIMARY or SECONDARY state. 要在副本集成员上运行,listCollections操作要求该成员处于PRIMARYSECONDARY状态。If the member is in another state, such as STARTUP2, the operation errors.如果成员处于另一种状态,如STARTUP2,则操作错误。

Example示例

The following returns the names of all collections in the records database:以下内容返回records数据库中所有集合的名称:

use records
db.getCollectionNames()

The method returns the names of the collections in an array:该方法返回数组中集合的名称:

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