Use Mongo Shell to Create a Database

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

If you’re planning to use MongoDB to store data, it’s important to know how to perform operations within the Mongo shell. The Mongo shell is an interactive interface for MongoDB that allows you to both manipulate data in your collections as well as perform many administrative operations. In this article, we’ll focus on the task of creating a database. It’s interesting to note that there’s actually no explicit command to create databases in MongoDB. This is because MongoDB is a NoSQL database that’s designed to create collections and databases on the fly. We’ll walk you through the steps needed to use Mongo shell to create a database.

Prerequisites

You’ll need to make sure MongoDB is installed on your device in order to follow along with this tutorial. To check the status of MongoDB, just start a Mongo shell instance in the command-line interface or the terminal.

Start MongoDB

MongoDB has an interactive interface that allows users to perform queries and other commands in a command-line console. Use the following command in a Linux terminal to start the mongodb service:

1
sudo service mongod start

NOTE: To check the status of MongoDB in Linux, just use the command sudo service mongod status.

You’ll need to enter the root password to gain the permission needed to access MongoDB.

Start MongoDB on macOS

If you’re using macOS, you can install MongoDB Community Edition using Homebrew. You’ll need to install Homebrew first, then you can update the repositories if needed using the following command:

1
brew update && brew doctor

Then use the following commands to tap the repository for MongoDB:

1
brew tap mongodb/brew

Finally, you can use the command shown below to install v4.2 of MongoDB which is the latest stable version as of January 2020:

1
brew install mongodb-community@4.2

Once MongoDB has finished installing, use the following brew services command to start the MongoDB server on your macOS machine:

1
brew services start mongodb-community@4.2

Access Mongo shell

Type the following command in a terminal or command prompt to access the Mongo shell:

1
mongo

This will create an instance which serves to confirm that MongoDB is started up. It will then initialize the Mongo shell, which is where we’ll create our database.

Use the --host and --port options with the mongo command if you’d like to specify a domain or IP address or a port other than the default 27017 port. You can see how this works in the following example:

1
mongo --host 123.45.678 --port 12345

List MongoDB databases

If you need a list of your existing databases, you can use the db.adminCommand() method call as seen below:

1
db.adminCommand('listDatabases')

Another way to get a list of databases is to use the show dbs command or call the getMongo().getDBNames() method:

1
db.getMongo().getDBNames()

The Mongo Client command shown above should return something like the following:

1
[ "admin", "config", "local", "dbName1", "dbName2" ]

The "admin", "config" and "local" databases are built-in MongoDB system databases.

create database mongo shell MongoDB list databases

Use Mongo shell to create a database

Although there’s no explicit CREATE DATABASE statement available, it’s not difficult to create a database in MongoDB. You’ll need to switch into a non-existent database and just insert the data into it. You can execute the USEcommand followed by a non-existent database name.

Let’s look at an example of this tactic in action:

1
2
> use book
switched to db book

We can verify that we’re in the selected database :

1
2
> db
book

NOTE: The non-existent database is not actually created until you will insert data into the database.

If you try to show and display all your databases in MongoDB, the non-existent book database will not show up alongside with other system databases:

1
2
3
4
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

Create a database with MongoDB insertOne()

At this point, we’re ready to use the database and insert some data:

1
2
> db.author.insert({ authorname: "Jan Tresh" })
WriteResult({ "nInserted" : 1 })

NOTE: In versions of MongoDB that are older than 3.x, you’ll need to use the insert() method. If you try to use insertOne(), you’ll get an error with the message that insertOne() is not a valid function.

Not only did the insert() method call create a database called book, but it also created a collection called author at the time of insertion.

Use mongo Shell to create a database with the MongoDB insertOne method

Use MongoDB findOne() to get the document

You can find a value that is stored in the database with a generated _id field, because MongoDB find() doesn’t require you to pass the BSON ID to its method call:

1
2
> db.author.find()
{ "_id" : ObjectId("5e0385d227b07499e9eee55e"), "authorname" : "Jan Tresh" }

NOTE: In newer versions of MongoDB you can also use the db.coll.findOne() method call to get the same results.

When you’re done, you can use the quit() command to exit out of the Mongo client. You’ll return to your root terminal or command prompt.

Conclusion

If you’re accustomed to working with SQL and relational databases, you may have been surprised to learn that there’s no CREATE DATABASE command in MongoDB. However, that’s because MongoDB is a NoSQL database and can handle all sorts of unstructured and loosely-structured data. Part of its flexibility lies in the fact that you don’t necessarily need to create databases– they’re created on the fly at the time of document insertion. In this article, we showed how it only takes a couple of simple steps in Mongo shell to create a database. With our instructions to assist you, you’ll be able to create new databases in your own MongoDB environment.

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.