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