Show collections in MongoDB

Introduction

When you’re working with multiple collections in MongoDB, you may need to retrieve a list of all the collections available in your database. Fortunately, it’s easy to obtain this information with a simple MongoDB command. In this article, we’ll explain how to show collections in MongoDB and walk through an example of how to use the show collections command.

Prerequisite

In order to get the most out of the examples in this tutorial, you’ll need to have MongoDB installed and configured on your machine.

What is MongoDB?

Let’s begin our discussion with a quick overview of MongoDB and its benefits. MongoDB is best described as an open source NoSQL database that can provide support for various forms of data. It’s designed to support high-volume storage for organizations. MongoDB offers high availability and easy scaling.

Show Collections in MongoDB

To obtain a list of MongoDB collections, we need to use the Mongo shell command show collections. This command will return all collections created within a MongoDB database. To be able to use the command, we’ll first need to select a database where at least one collection is stored.

Let’s select a database with the use command:

1
use <sampledatabase>

Next, we can execute the show collections command:

1
show collections

This command will list all the collections that were created inside the selected MongoDB database.

Creating a MongoDB Collection

In this section, we’ll be creating a sample collection that we can use for this tutorial.

To do this, we’ll use the following command: db.createcollection(<name_of_document>, <options>)

Remember that a MongoDB collection is composed of a group of documents.

Shown below are a few key points to keep in mind when creating a collection:

  • Name : We need to provide a meaningful name for the collection that we’re going to create.

  • Options : The options we specify serve as the configuration for the MongoDB collection.

  • Capped : This optional flag can be used if we want to create a capped collection that has a specified size. When the size of a capped collection reaches the max size, the entire collection will be overwritten to allow for storage of the new data.

  • Size : When we enable the cap parameter, we also need to set the size parameter. This value indicates the maximum size of the collection in bytes.

  • Max : This parameter sets the max documents for a capped collection.

Insert Record into MongoDB

In this section, we’ll show you how to create a MongoDB collection and insert a document at the same time. The basic syntax of this command is shown below:

1
db.<collectionName>.insert({<key> : <value>, <key> : <value>})

Let’s start the process of creating our collection:

1
use employee

This command selects the database employee. We’ll receive a response that says switched to db employee.

Now let’s create our collection and insert a document simultaneously:

1
2
3
4
5
db.employeeprofile .insert({
   emp_name: "Pradeep",
   emp_age:  23,
   emp_website: "employeeprofile.com"
})

After executing this command, MongoDB will notify us: WriteResult({ "nInserted" : 1 }).

We can verify that this operation was successful using the command shown below: db.employeeprofile.find().pretty();

We should see the following result:

1
2
3
4
5
6
{
        "_id" : ObjectId("5e3e18de7ae22d364e36bb76"),
        "emp_name" : "Pradeep",
        "emp_age" : 23,
        "emp_website" : "employeeprofile.com"
}

We can see that the documents were successfully inserted and our ’employee’ collection was also created.

Now that we have a collection stored in a database, we can finally test out the show collections command. Let’s use it to obtain a list of all existing collections in our target database:

1
2
> show collections
employeeprofile

As you can see, the command listed the newly-created collection.

Conclusion

When you need to find out which collections are stored in a MongoDB database, it’s important to know how to obtain this information. It’s easy to show collections in MongoDB using a simple shell command. In this article, we created a sample collection and then used the show collections command to retrieve its name. With this example to use as a guide, you’ll be able to obtain a list of collections from your own MongoDB database.

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.