listCollections
Definition
listCollections
Retrieve information, i.e. the name and options, about the collections and views in a database. Specifically, the command returns a document that contains information with which to create a cursor to the collection information.
mongosh
provides thedb.getCollectionInfos()
and thedb.getCollectionNames()
helper methods.
Syntax
The command has the following syntax:
db.runCommand( { listCollections: 1, filter: <document>, nameOnly: <boolean>, authorizedCollections: <boolean>, comment: <any> } )
Command Fields
The command can take the following optional fields:
Field | Type | Description |
---|---|---|
filter | document | Optional. A query expression to filter the list of collections. You can specify a query expression on any of the fields
returned by |
nameOnly | boolean | Optional. A flag to indicate whether the command should return just the collection/view names and type or return both the name and other information. Returning just the name and type ( The default value is NoteWhen |
authorizedCollections | boolean | Optional. A flag, when set to When both The default value is For a user who has When used without |
comment | any | Optional. A user-provided comment to attach to this command. Once set, this comment appears alongside records of this command in the following locations:
A comment can be any valid BSON type (string, integer, object, array, etc). New in version 4.4. |
Behavior
Filter
Use a filter to limit the results of listCollections
. You
can specify a filter
on any of the fields returned in the listCollections
result
set.
Locks
The listCollections
command takes Intent Shared lock on the
database. In previous versions, the command takes Shared lock on the
database.
Unless the nameOnly
option is specified, the command also takes an
Intent Shared lock on each of the collections in turn while holding the
Intent Shared lock on the database.
Client Disconnection
Starting in MongoDB 4.2, if the client that issued listCollections
disconnects before the operation completes, MongoDB marks listCollections
for termination using 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. If the member
is in another state, such as STARTUP2
, the
operation errors.
In previous versions, the operations also run when the member
is in STARTUP2
. The operations wait until the member
transitioned to RECOVERING
.
Required Access
To run listCollections
when access control is enforced,
users must, in general, have privileges that grant
listCollections
action on the database. For example,
the following privilege grants users to run
db.getCollectionInfos()
against the test
database:
{ resource: { db: "test", collection: "" }, actions: [ "listCollections" ] }
The built-in role read
provides the privilege to run
listCollections
for a specific database.
Users without the required privilege
can run the command with both authorizedCollections
and
nameOnly
options set to true
. In this case, the command returns
just the name and type of the collection(s) to which the user has
privileges.
For example, consider a user with a role that grants just the following privilege:
{ resource: { db: "test", collection: "foo" }, actions: [ "find" ] }
The user can run the command if the command includes both
authorizedCollections
and nameOnly
options set to true
(with or without the filter
option):
db.runCommand( { listCollections: 1.0, authorizedCollections: true, nameOnly: true } )
The operation returns the name and type of the foo
collection.
However, the following operations (with or without the filter
option) error for the user without the required access:
db.runCommand( { listCollections: 1.0, authorizedCollections: true } ) db.runCommand( { listCollections: 1.0, nameOnly: true } )
show collections
The mongo
shell, show collections
is equivalent to:
db.runCommand( { listCollections: 1.0, authorizedCollections: true, nameOnly: true } )
For users with the required access,
show collections
lists the non-system collections for the database.For users without the required access,
show collections
lists only the collections for which the users has privileges.
Output
listCollections.cursor
A document that contains information with which to create a cursor to documents that contain collection names and options. The cursor information includes the cursor id, the full namespace for the command, as well as the first batch of results. Each document in the batch output contains the following fields:
FieldTypeDescriptionnameStringName of the collection.typeStringType of data store. Returnscollection
for collections,view
for views, andtimeseries
for time series collection.optionsDocumentCollection options.
These options correspond directly to the options available in
db.createCollection()
. For the descriptions on the options, seedb.createCollection()
.infoDocumentLists the following fields related to the collection:
- readOnly
boolean
. Iftrue
the data store is read only.- uuid
- UUID. Once established, the collection UUID does not change. The collection UUID remains the same across replica set members and shards in a sharded cluster.
idIndexDocumentProvides information on the_id
index for the collection.
Example
List All Collections
The following example uses the db.getCollectionInfos()
helper
to return information for all collections in the records
database:
use records db.getCollectionInfos();