For system, network and cloud administrators

How to create a MongoDB database

Once you’ve installed MongoDB on your system, these are the steps for creating a MongoDB.

Ubuntu 20.04 LTS

  1. Check if the mongod service is running first:
sudo systemctl status mongod.service

If the service is not running then you’ll need to start the service and check the status of it again:

sudo systemctl start mongod.service
sudo systemctl status mongod.service
  1. Launch the MongoDB console just by typing in your terminal:
  1. In order to tell the service we’re going to create a database, we’ll first need to type:
> use a_name_of_your_choosing;

The console will then tell you it switched to the database you’ve just created. But to test that at this point you’re using the correct database, type:

  1. Check if you’re using the proper database:
> db;

The name of your database should be the output. Now, in order to be able to actually start using this newly created database, you’ll need to actually insert data into your MongoDB database so that the service will be able to display it to you when using the show dbs; command. This means that the following step is crucial in order to finalize the creation of your MongoDB database.

  1. Insert your first data into your newly created database:
> db.favoriteanimals.insert({"name":"Foxy fox"});

The output of the command above should be similar to: WriteResult({ "nInserted" : 1 }).

  1. Check the existing of your database by typing:
> show dbs;

If the name of your database is shown in the output then you’ve successfully created your first MongoDB database.

Leave a Reply

Your email address will not be published. Required fields are marked *