db.getCollectionNames()

On this page本页内容

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

Considerations注意事项

Changed in version 4.0.在版本4.0中更改

db.getCollectionNames() no longer locks the collections to return name information.不再锁定集合以返回名称信息。

Required Access所需访问权限

Starting in version 4.0 of the mongo shell, db.getCollectionNames() is equivalent to:从mongoshell 4.0版开始,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.例如,如果用户在数据库中找到了特定集合,则该方法将仅返回该集合。

Behavior行为

Client Disconnection客户端断开连接

Starting in MongoDB 4.2, if the client that issued the db.getCollectionNames() disconnects before the operation completes, MongoDB marks the db.getCollectionNames() for termination (i.e. killOp on the operation).从MongoDB 4.2开始,如果发出db.getCollectionNames()的客户端在操作完成之前断开连接,MongoDB将db.getCollectionNames()标记为终止(即操作上的killOp)。

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

Starting in MongoDB 4.4, to run on a replica set member, listCollections operations require the member to be in PRIMARY or SECONDARY state. 从MongoDB 4.4开始,要在副本集成员上运行,listCollections操作要求该成员处于PRIMARYSECONDARY状态。If the member is in another state, such as STARTUP2, the operation errors.如果成员处于其他状态,如STARTUP2,则操作出错。

In previous versions, the operations can also be run when the member is in STARTUP2. 在以前的版本中,也可以在成员处于STARTUP2时运行操作。However, the operations wait until the member transitions to RECOVERING.但是,这些操作将一直等到成员转换到RECOVERING

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" ]
←  db.getCollectionInfos()db.getLogComponents() →