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:

db.runCommand( { listCollections: 1.0, authorizedCollections: true, nameOnly: true } )

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).

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. If the member is in another state, such as STARTUP2, the operation errors.

In previous versions, the operations can also be run when the member is in STARTUP2. However, the operations wait until the member transitions to RECOVERING.

Example示例

The following returns the names of all collections in the records database:

use records
db.getCollectionNames()

The method returns the names of the collections in an array:

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