The database is now available to you. You can manually enter new documents, or connect to the database using any of the MongoDB drivers.
Using the MongoDB Shell
Like most complex software systems, MongoDB can be controlled with what is called a command-line interface, often referred to as a CLI.
By entering commands into the CLI, you tell MongoDB how to operate, get information about how the MongoDB cluster is running, and perform fundamental actions like the one we will cover today: creating a database.
To create a database using a command-line interface, the first task is to get access to the MongoDB cluster you are using via the MongoDB Shell. A shell is a program that allows you to enter commands into a software system.
Prerequisites for using the CLI with MongoDB Atlas
If you are using MongoDB Atlas, the steps to getting a shell are as follows:
- Add your IP to the IP access list for your Atlas project
- Make sure you have a database user on the MongoDB cluster you want to use
- Make sure you have the MongoDB Shell installed on your machine
- Open a terminal emulator, run the mongosh command, and log in to the MongoDB Atlas cluster
Find out more at Connect to Atlas via MongoDB Shell
Prerequisites for using the CLI with a self-managed MongoDB cluster
If you are running a self-managed cluster of MongoDB:
- Make sure the MongoDB self-managed cluster is installed and running on your computer or the computer you are going to connect to
- Make sure you have a database user on the MongoDB cluster you want to use
- Make sure the MongoDB Shell is installed on your computer
- Open a terminal, run the mongosh command and log in to the MongoDB self-managed cluster
Find out more at Connect to a Deployment from the MongoDB Shell
Creating a MongoDB database with the CLI
Once you have access to a cluster via the MongoDB Shell, you can see all the databases in the cluster that you have access to using the “show” command:
Note that admin and local are databases that are part of every MongoDB cluster.
There is no “create” command in the MongoDB Shell. In order to create a database, you will first need to switch the context to a non-existing database using the use command:
Note that, for now, only the context has been changed. If you enter the show dbs command, the result should still be the same:
Wait a second. Where’s myshinynewdb?
MongoDB only creates the database when you first store data in that database. This data could be a collection or a document.
To add a document to your database, use the db.collection.insert() command.
The “user” in the command refers to the collection that the document was being inserted in.
Collections can be created just like databases, by writing a document to them. They can also be created using the createCollection command.
WriteResult({ "nInserted" : 1 }) indicates that the document was added to the collection.
Now, if you run the show dbs command, you will see your database.
There’s one more thing.
How did the insert command know to put the data into myshinynewdb?
It turns out that when you entered the use command, then myshinynewdb became the current database on which commands operate.
To find out which database is the current one, enter the db command:
The db command displays the name of the current database. To switch to a different database, type the use command and specify that database.
Using the MongoDB Compass
Some users would rather work with a GUI to create and update their data and collections. The MongoDB GUI, Compass, offers additional functionality like data visualization and performance profiling as well as offering CRUD (create, read, update, delete) access to data, databases, and collections.
Find out more at MongoDB Compass: The Easiest Way to Manage and Explore Your Data
Prerequisites for using Compass with MongoDB Atlas
If you are using MongoDB Atlas, the steps to getting to Compass are as follows:
- Add your IP to the IP access list for your Atlas project
- Make sure you have a database user on the MongoDB cluster you want to use.
- Make sure you have MongoDB Compass installed. If not, download and install Compass for your operating system.
Prerequisites for using Compass with a self-managed MongoDB cluster
If you are using self-managed MongoDB:
- Make sure the MongoDB self-managed cluster is installed and running on your machine or server
- Make sure you have a database user on the MongoDB cluster you want to use
Make sure you have MongoDB Compass installed on your computer. If not, download and install Compass for your operating system.
Creating a MongoDB database with Compass
The Databases tab in MongoDB Compass has a "Create Database" button.