db.getCollectionNames()
Definition
db.
getCollectionNames
()- Returns an array containing the names of all collections andviews in the current database, or if runningwith access control, the names of the collections according to user’sprivilege. For details, see Required Access.
Considerations
Changed in version 4.0: db.getCollectionNames()
no longer locks the collections toreturn name information.
For MongoDB 3.0 deployments using the WiredTiger storage engine, if you run db.getCollectionNames()
from aversion of the mongo
shell before 3.0 or a version of thedriver prior to 3.0 compatible version, db.getCollectionNames()
will return no data, even ifthere are existing collections. For more information, seeWiredTiger and Driver Version Compatibility.
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 } )
- For users with the required access (privileges that grant
listCollections
action on the database), the methodlists the names of all collections for the database. - For users without the required access, the method lists only thecollections for which the users has privileges. For example, if auser has
find
on a specific collection in adatabase, 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 marksthe db.getCollectionNames()
for termination (i.e. killOp
on theoperation).
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" ]