On this page本页内容
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.有关详细信息,请参阅必需访问。
Changed in version 4.0.在版本4.0中更改。
db.getCollectionNames()
Starting in version 4.0 of the 从mongoshell 4.0版开始,mongo
shell, db.getCollectionNames()
is equivalent to:db.getCollectionNames()
相当于:
db.runCommand( { listCollections: 1.0, authorizedCollections: true, nameOnly: true } )
listCollections
action on the database), the method lists the names of all collections for the database.listCollections
操作的权限)的用户,该方法将列出数据库的所有集合的名称。find
on a specific collection in a database, the method would return just that collection.Starting in MongoDB 4.2, if the client that issued the 从MongoDB 4.2开始,如果发出db.getCollectionNames()
disconnects before the operation completes, MongoDB marks the db.getCollectionNames()
for termination (i.e. killOp
on the operation).db.getCollectionNames()
的客户端在操作完成之前断开连接,MongoDB将db.getCollectionNames()
标记为终止(即操作上的killOp
)。
Starting in MongoDB 4.4, to run on a replica set member, 从MongoDB 4.4开始,要在副本集成员上运行,listCollections
operations require the member to be in PRIMARY
or SECONDARY
state. listCollections
操作要求该成员处于PRIMARY
或SECONDARY
状态。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
。
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" ]