How to list all collections from mongo shell ?
This tutorial guides you the ways to list all collections from mongo shell. The mongo shell provides helper methods like db.getCollectionInfos() and db.getCollectionNames().
list all collections from mongo shell
The db.getCollectionInfos() helper wraps the listCollections command. The listCollections command has the following form.
{ listCollections: 1, filter: <document>, nameOnly: <boolean>, authorizedCollections: <boolean> }
Therefore, db.getCollectionInfos(filter, nameOnly, authorizedCollections) returns an array of documents with collection information, such as name, type, options etc., for the current database and the results depends on the privileges that user has.
The user can run the following command which includes both nameOnly and authorizedCollections options set to true and with or without the filter option as shown below. Also you can run db.getCollectionInfos() without optional parameters nameOnly and authorizedCollections as shown.
> db.getCollectionInfos({},true,true) Response --------- [ { "name" : "items", "type" : "collection" }, { "name" : "names", "type" : "collection" }, { "name" : "posts", "type" : "collection" } ] > db.getCollectionInfos() Response -------- [ { "name" : "items", "type" : "collection", "options" : { }, "info" : { "readOnly" : false, "uuid" : UUID("852f9fd6-d5a0-4faf-a5a0-9f5c8ee42d76") }, "idIndex" : { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "sneppets.items" } }, { "name" : "names", "type" : "collection", "options" : { }, "info" : { "readOnly" : false, "uuid" : UUID("92c41f26-12a6-4619-8db4-7d11a849afad") }, "idIndex" : { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "sneppets.names" } }, { "name" : "posts", "type" : "collection", "options" : { }, "info" : { "readOnly" : false, "uuid" : UUID("2ea209b1-ea07-4d18-8971-ac1071baa881") }, "idIndex" : { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "sneppets.posts" } } ]
Note, since db.getCollectionsInfos() is a wrapper around the listCollections, users should have the same privileges as listCollections.
listCollections command
You can also list all collections using listCollections command with db.runCommand(<listCollections command>) in mongo shell as shown below.
> db.runCommand({listCollections: 1.0, filter:{}, nameOnly: true, authorizedCollections: true}) Response: --------- { "cursor" : { "id" : NumberLong(0), "ns" : "sneppets.$cmd.listCollections", "firstBatch" : [ { "name" : "names", "type" : "collection" }, { "name" : "items", "type" : "collection" }, { "name" : "posts", "type" : "collection" } ] }, "ok" : 1 }
show collections
Starting from MongoDB version 4.0 in mongo shell when you type show collections it is equivalent to the above listCollections command with db.runCommand(<listCollections command>) which was just explained.
> show collections Response -------- items names posts
list all collections using db.getCollectionNames()
db.getCollectionNames() is an helper method which also wraps listCollections command like db.getCollectionInfos() and returns an array containing the names of all collections in the current database as shown below.
> db.getCollectionNames() Response --------- [ "items", "names", "posts" ]
Note, from version 4.0 db.getCollectionNames() is equivalent to:
db.runCommand( { listCollections: 1.0, authorizedCollections: true, nameOnly: true } )
List all collections from command line
Finally, to list all collections from command prompt you need to use the –eval option to mongo to pass the shell a JavaScript part like mongo <database> –eval “db.getCollectionNames()” i.e., from the command line , use mongo to evaluate JavaScript as shown below.
C:\Program Files\MongoDB\Server.0\bin>mongo sneppets --eval "db.getCollectionNames()" Response --------- MongoDB shell version v4.0.6 connecting to: mongodb://127.0.0.1:27017/sneppets?gssapiServiceName=mongodb Implicit session: session { "id" : UUID("c4b102a4-a99c-4ab4-9188-7508ec99f82a") } MongoDB server version: 4.0.6 [ "items", "names", "posts" ]
Also Read:
- Guide to sort numeric string values as numbers in mongodb
- MongoDB – Advantages and When to Use ?
- How to import CSV using mongoimport command in MongoDB