How to Create a Database in MongoDB

So you want to try MongoDB, but you’re not sure how to create a database. You’re in the right place.

In this article, we’ll cover how to create a database using the MongoDB Atlas user interface (user interface), MongoDB Shell (mongosh), or using Compass, the MongoDB GUI. All methods are available for MongoDB Atlas, the MongoDB developer data platform, and for self-managed clusters.

Table of Contents

Using the MongoDB Atlas User Interface Using the MongoDB Shell Using MongoDB Compass Using the MongoDB Atlas User Interface The easiest way to get started with

  • MongoDB

is through the Atlas developer data platform. A free tier with basic database functionality is available. This free tier is more than enough for the purposes of this article.

Prerequisites for MongoDB Atlas To create a database in MongoDB Atlas, you’ll need to register an Atlas account and create your first forever free cluster:Register a free Atlas account with your

  • email address (no credit card required)
  • Deploy your first cluster in less than 10 minutes

Create a MongoDB database using the Atlas user interface From the

cluster page,

click “Browse Collections”.

<img src="https://webimages.mongodb.com/_com_assets/cms/l8sx7eoqk4klqwad9-First%20Cluster.png" alt="Screenshot of MongoDB Atlas dashboard showing database deployments.

” />

If there are no databases in this cluster, you will be presented with the option to create your first database by clicking the “Add my own data” button.

Screenshot of the MongoDB Atlas dashboard on the Collections tab.

This will open a modal, asking for a database name and a collection name. Once these fields are completed, click “Create” and your database will be created for you.

Video demonstration of creating a new database in Atlas.

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’s called a command-line interface, often referred to as a CLI.

By entering commands into the CLI, it tells MongoDB how to operate, gains insight into how the MongoDB cluster is running, and performs fundamental actions like the one we’ll cover today: Create a database.

To create a database by using a command-line interface, the first task is to access the MongoDB cluster that you are using through 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 get a shell

are as follows:

Add your IP to the

  • IP access list for your Atlas project
  • Make sure you have a database user in the MongoDB cluster that you want to use

  • make sure you have the mongodb shell installed on your computer
  • Open a terminal emulator, run the mongosh command and log in

to the MongoDB Atlas cluster

Learn 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 MongoDB cluster:Make sure that the MongoDB self-managed cluster is installed and running on the computer or computer to which you are connecting Make sure you have a database in the MongoDB cluster

    you want to use Make sure MongoDB

  • Shell is installed on your computer
  • Open a terminal, run the mongosh command, and log in
  • to the MongoDB self-managed

  • cluster
  • Learn more at Connect to a deployment from the MongoDB shell Create a

  • MongoDB

database

with the CLI

Once you access a cluster through the MongoDB shell, you can view all the databases in the 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 each

MongoDB cluster.

There is no “create” command in the MongoDB shell. To create a database, you must first change the context to a non-existent database using the use:> use

myshinynewdb command

Note that, for now, only the context has been changed. If you enter the show dbs command, the result should remain the same:> show dbs

admin 0.000GB local 0.000GB

Wait for a second. Where is myshinynewdb?

MongoDB only creates the database when it first stores data in that database. This data can be a collection or a document.

To add a document to the database, use the db.collection.insert() command.

> db.user.insert({name: “Ada Lovelace”, age: 205}) WriteResult({ “nInserted” : 1 })

The “user” in the command refers to the collection into which the document was inserted

.

Collections can be created just like databases by writing a document to them. They can also be created by 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.

> show dbs admin 0.000GB myshinynewdb 0.000GB local 0.000GB There is

one more thing

.

How did the insert command know how to put the data in

myshinynewdb?

It turns out that when you entered the use command, myshinynewdb became the current database in which commands operate. To

find out which

database is current, type 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.

Using the GUI, MongoDB Compass

Some users prefer to work with a GUI to create and update their data and collections. MongoDB’s GUI, Compass, offers additional functionality such as data visualization and performance profiles, as well as providing CRUD (create, read, update, delete) access to data, databases, and collections.

Learn 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 get to Compass are as follows:

Add your IP to the IP

access list

  • for your Atlas project
  • Make sure that you have a database user in the MongoDB cluster that 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 use self-managed MongoDB:Make sure the MongoDB

self-managed

cluster is installed and running on the computer or server Make sure that you have

    a database user in the MongoDB cluster that 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.

alt text tbd

In MongoDB Compass, create a database and add its first collection at the same time:

Click “Create Database”

  • to open the Enter dialog box
  • Name of the database and its first collection

  • Click
  • on “Create Database”The next step is to insert one or more documents into

  • the database

.

Click your database name to view the collection

you created, then click the collection name to view the Documents tab:

alt text tbd

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 JSON documents separated by commas 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.

Learn more about MongoDB

Using these simple steps shows you how to create a database, collection, and insert documents.

You now have a database, collection and documents to work with and can learn even more. Join the MongoDB community for free MongoDB University forums, webinars, and courses.

Related content

: What is replication in MongoDB

    ?

  • Beginner’s Guide:
  • MongoDB Basics Using ACID transactions in MongoDB

  • Embedding documents in
  • MongoDB MongoDB Examples

  • The 5 Best Practices for
  • MongoDB
  • Security