MongoDB – Create Database And Collection

This post describes how to use the command prompt to connect to a MongoDB server and use CLI commands to display the available databases and collections, create a database and create a collection. To put it another way, it explains the fundamental CLI commands.

Connecting to MongoDB:

You can simply type “mongo.exe” in your run command if “mongo.exe” is included to your class-path of environment variables. Otherwise, look in your computer’s “bin” folder to see where it is.

Change the location where your “mongo.exe” is stored in the command prompt. It’s usually found under “C:\Program Files\MongoDB\Server\5.0\bin”. Depending on your setup, change the location of the bin folder.

C:\Program Files\MongoDB\Server\5.0\bin> mongo.exe

If no login is required, the above command will connect you to the MongoDB shell.

> show dbs
Or you can use “show databases”. This command will help in displaying available databases in the server.

The MongoDB shell lacks a “create database” command. To build a database, use the “USE” command to change the context to a non-existing database, as demonstrated below:

> use CompanyDB

The command above will bring the database “CompanyDB” into use.  Please keep in mind that MongoDB only creates the database when you store data in it for the first time. This data could be in the form of a collection or a document.

> show collections
The command above will let you list all of the collections (tables) in the database you connected to. Because no collections have been added to “CompanyDB,” the list will be empty. Let’s create a collection.

> db.createCollection(“emp”, { capped : true, size : 5242880, max : 5000 } )

Capped collections have maximum size or document counts that prevent them from growing beyond maximum thresholds. All capped collections must specify a maximum size and may also specify a maximum document count. MongoDB removes older documents if a collection reaches the maximum size limit before it reaches the maximum document count.

Executing the above command will create a collection named “emp” with a maximum size of 5 megabytes and a maximum of 5000 documents. If you do not want to specify any limitations, then you can simply use –

> db.createCollection(“emp2”)

Now, list out the collections
> show collections;


If you are using “Compass” this can be done in more simpler way. Creating the database and the collection can be done at the same time as shown below.

  • Open the Compass and Connect to the instance.
  • Click on the instance name on the navigation bar, you’ll see “Create Database” button.
  • Click on the “Create Database” button and enter the name of the database and its first collection.
  • Click Create Database


Hope you find this article helpful.

Please subscribe for more interesting updates.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s