MongoDB Show Collections - How to Show Collections in MongoDB with Examples

Introduction

In this article we’ll show you how to show a list of the collections in a database in MongoDB. There are different ways to do this but we’ll first show you the quick and easy way before we show you the other. We’ll show how to do it in the MongoDB Shell because that is the most universal way to do it. But you might be looking for how to do it using your drivers for NodeJS, PHP, or Python so we’ll also show you how to do it with the official drivers for NodeJS so you can see how it translates to the drivers.

Prerequisites

  • You should have MongoDB installed on your system.
  • You can follow along using the MongoDB Shell or with whatever driver you’re using.

Show Collections Using the MongoDB Shell

Let’s first go over how to list all the collections in a database using the MongoDB shell.

show collections

Yup, it’s that simple. This command will show you all the collections in the current database. If you’re not seeing the collection you expect to see, you may want to check if you have permissions because only the collections are listed for which you have permission.

Note: If you’re not sure how to start the MongoDB Shell please do the following which will get you into the MongoDB Shell as long as you have MongoDB installed. First go to a shell and run the mongod command which will run MongoDB in the background. Then in another terminal tab run the mongo command which will take you into the MongoDB Shell and prompt you with >.

A Common Workflow

Typically you’ll be checking to see if a collection exists in a database so let’s do the process end to end.

First let’s list the databases:

> show dbs
admin             0.000GB
grocerydb         0.000GB
librarydb         0.000GB
test              0.000GB

We are looking to see what collections exist in the grocerydb so we need to select that database with the use command:

> use grocerydb
switched to db grocerydb

Now that we’ve selected the database let’s list the collections in it with the command we just reviewed:

> show collections
products
users

An alternative: listCollections

You can also use one of the Administrative commands although the syntax is more obscure:

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

{
        "cursor" : {
                "id" : NumberLong(0),
                "ns" : "grocerydb.$cmd.listCollections",
                "firstBatch" : [
                        {
                                "name" : "products",
                                "type" : "collection"
                        }
                ]
        },
        "ok" : 1
}

NodeJS Example

The reason we introduced the Alternative way with the Administrative command is so that the drivers use a syntax more similar to the alternative. Let’s see how we could list our collections from a script in NodeJS:

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";

MongoClient.connect(url, function (err, db) {
    if (err) throw err;
    var dbo = db.db("grocerydb");
    dbo.listCollections().toArray(function (err, collectionInfos) {
        console.log(collectionInfos);
        db.close();
    });
});

If you run this script with your it will output an array that has info on each collection. The collectionInfos is an array of objects that contains info on each collection. When we ran this script on our grocerydb with two collections, this is the result we got:

[ { name: 'products',
    type: 'collection',
    options: {},
    info: { readOnly: false, uuid: [Binary] },
    idIndex:
     { v: 2, key: [Object], name: '_id_', ns: 'grocerydb.products' } },
  { name: 'users',
    type: 'collection',
    options: {},
    info: { readOnly: false, uuid: [Binary] },
    idIndex: { v: 2, key: [Object], name: '_id_', ns: 'grocerydb.users' } } ]

There’s a lot of info here but you can see from the name values that it returned info on both our collections.

Conclusion

We hope you were able to use one of these methods to listing your collections in MongoDB. We first showed you a couple options in the shell and then one using the NodeJS drivers from MongoDB. If you’re using a different set of drivers the code should be similar.

We hope this solved any issue you may have had but if you want to move to having your MongoDB data and the complexity of a production environment managed by experts, don’t hesitate to reach out to us at Object Rocket.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.