When learning a new technology, sometimes the hardest things are the simple steps that take place to get started. These are the steps that “everybody knows” but are often not discussed.
This article covers a fundamental step in using MongoDB: Creating a database.
You can create your MongoDB database, with either the mongo shell, or using Compass. Both methods are available for MongoDB Atlas clusters, and for self-managed clusters.
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.
MongoDB also has a graphic user interface, a GUI called Compass, that can be used to perform many of the same tasks as the commands in the CLI. We’ll cover creating a database using Compass in a later section.
So, if you want 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.
If you are using MongoDB Atlas, the steps to getting a shell are as follows:
mongo
command to start the shell and log in to the MongoDB Atlas cluster.Find out more at Connect to Atlas via Mongo Shell
If you are running a self-managed cluster of MongoDB:
mongo
command and log in to the MongoDB self-managed cluster.Find out more at Using the MongoDB Shell in Community Edition
Once you have access to a cluster via the MongoDB shell, you can see all the databases in a cluster that you have access to using the “show” command:
> show dbs
admin 0.000GB
local 0.000GB
Note that admin
and local
are databases that are part of every MongoDB cluster.
From here on out there are only two tricky things to remember.
The first
is that there isn’t a “create” command in the MongoDB shell.
To create a database you use the use
command. If the database doesn’t exist, then the MongoDB cluster will create it.
In other words, a database is created when you try to use it with the “use
” command.
But let’s say you entered the following command to create a new database:
use myshinynewdb
That’s all you have to do. The database is created. But that leads us to the second tricky thing. Even though the database exists, if you enter the show dbs
command it will look like this:
> show dbs
admin 0.000GB
local 0.000GB
Wait a second. Where’s myshinynewdb
?
The second tricky thing is that the database isn’t fully created until you put something into it.
To add a document to your database, use the db.<collection>.insert
command.
> db.user.insert({name: "Ada Lovelace", age: 205})
WriteResult({ "nInserted" : 1 })
A couple of notes. The “user
” in the command refers to the collection that the document was being inserted in. Collections in MongoDB are like tables in a SQL database, but they are groups of documents rather than groups of records.
Collections are created just like databases, by referring to them in a 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.
> show dbs
admin 0.000GB
myshinynewdb 0.000GB
local 0.000GB
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:
> db
myshinynewdb
The db
command displays the name of the current database. To switch to a different database, type the use
command and specify that database.
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
If you are using MongoDB Atlas, the steps to getting to Compass are as follows"
If you are using self-managed MongoDB:
The Databases tab in MongoDB Compass has a Create Database button.
In MongoDB Compass, you create a database and add its first collection at the same time:
Find out more here
The next step is to insert one or more documents into your database.
Click on your database’s name to see the collection you created, then click on the collection’s name to see the Documents tab:
Click the Add Data button to insert one or more documents into your collection.
You can add JSON documents one at a time, or add multiple documents in an array by enclosing comma-separated JSON documents in square brackets, as shown in this example:
[
{ "_id" : 8752, "title" : "Divine Comedy", "author" : "Dante", "copies" : 1 },
{ "_id" : 7000, "title" : "The Odyssey", "author" : "Homer", "copies" : 10 },
{ "_id" : 7020, "title" : "Iliad", "author" : "Homer", "copies" : 10 },
{ "_id" : 8645, "title" : "Eclogues", "author" : "Dante", "copies" : 2 },
{ "_id" : 8751, "title" : "The Banquet", "author" : "Dante", "copies" : 2 }
]
Click Insert to add the documents to your collection.
Using these simple steps shows how to create a database, a collection, and insert documents.
Now you have a database, collection, and documents to work with and can learn even more. Join the MongoDB community for forums, webinars, and free courses from MongoDB University.