HOWTO · MongoDB
How to List Collections in MongoDB
Learn how to list MongoDB collections in mongosh with show collections, getCollectionNames, listCollections, and getCollectionInfos.
On this page
MongoDB stores documents in collections. When you need to see which collections exist in a database, use one of the mongosh commands or helper methods below. The shortest answer is show collections; the other options return a JavaScript array or collection metadata that you can filter.
Select the database you want to inspect
Collection-listing commands operate on the current database. Select it before running any of the examples:
use catalog
Replace catalog with your database name. Database names are case-sensitive. If you are unsure which databases exist on the server, run show dbs first, then switch to the database that you want to inspect.
Use show collections for a quick list
Run this interactive mongosh command when you only need a readable list:
show collections
The output depends on the collections and views in the current database. mongosh can identify views and time-series collections in its output. With the required permissions, show collections lists the database’s non-system collections. With limited permissions, it lists only collections that the user can access.
Names such as system.views are generated by MongoDB to support views and are not application collections.
Use this command for a quick check at the shell prompt. It is not the best choice when a script needs an array, when you need collection options, or when you need to filter the result.
Return collection names with db.getCollectionNames()
db.getCollectionNames() returns an array containing the names of collections and views in the current database:
db.getCollectionNames()
For example, a database containing two collections and one view can return an array like this:
[ "activeProducts", "clients", "products", "system.views" ]
The returned order is not a reliable sort order. Sort the array in your JavaScript code if the order matters:
db.getCollectionNames().sort()
This helper is useful when you want names for a loop or a small shell script. It does not return collection options or tell you whether each name represents a regular collection, a view, or a time-series collection.
Use listCollections for the raw command response
The listCollections database command returns a cursor containing collection and view information. Use nameOnly: true when you need only each name and type:
db.runCommand({
listCollections: 1,
nameOnly: true,
authorizedCollections: true
})
The cursor.firstBatch documents identify each data store with a name and a type, such as collection, view, or timeseries. Remove nameOnly: true when you need fields such as collection options, read-only status, a UUID, or _id index information. The command returns an unsorted list, so sort the result in the client if you need stable ordering.
authorizedCollections: true has an effect when it is used together with nameOnly: true. It allows a user without the database-level listCollections privilege to see the names and types of collections for which that user has privileges. Without the required access, the full metadata form can fail with an authorization error.
Inspect or filter metadata with db.getCollectionInfos()
db.getCollectionInfos(filter, options) is the mongosh helper for metadata-rich collection inspection. To return only names and types, pass nameOnly inside the options document:
db.getCollectionInfos({}, { nameOnly: true })
Example output from a database containing a view and two collections is:
[{"name":"activeProducts","type":"view"},{"name":"clients","type":"collection"},{"name":"products","type":"collection"},{"name":"system.views","type":"collection"}]
To inspect one collection, filter by its name:
db.getCollectionInfos(
{ name: "clients" },
{ nameOnly: true }
)
[{"name":"clients","type":"collection"}]
If the filter matches no collection, the method returns an empty array:
db.getCollectionInfos(
{ name: "missing" },
{ nameOnly: true }
)
[]
Omit nameOnly when you need metadata such as options, info.readOnly, info.uuid, or idIndex. UUIDs are generated for a particular collection, so do not copy one from an example into documentation or a test expectation. You can filter on fields returned by the metadata response, for example { "info.readOnly": true }.
Choose the right collection-listing method
| Need | Use |
|---|---|
A quick human-readable list in mongosh |
show collections |
| A JavaScript array of names | db.getCollectionNames() |
| A raw database command and cursor | db.runCommand({ listCollections: 1, ... }) |
| Collection/view metadata or a filter | db.getCollectionInfos(filter, options) |
The commands above are for the current mongosh shell. Older tutorials may use the legacy mongo shell; use mongosh for current MongoDB installations. The available names and metadata still depend on the selected database and the user’s privileges. On a replica-set member, listCollections operations require the member to be in the PRIMARY or SECONDARY state.
For the complete field list and access rules, see the MongoDB documentation for listCollections, db.getCollectionInfos(), and db.getCollectionNames().