While an SQL database engine organizes data into tables, MongoDB stores its data in collections. Like traditional engines, though, it still uses the term "database" to refer to the highest level of organization.

One of the first tasks you'll want to learn when working with any database program is how to create databases and tables/collections. If you're using MongoDB, read on to discover exactly how to create a database and insert collections into it.

Initial MongoDB Setup

You can create databases and collections in MongoDB using the MongoDB Shell, mongosh. You can download and install this as a dedicated tool.

Alternatively, you can access it using MongoDB's desktop app, MongoDB Compass. It's a versatile GUI tool that lets you see databases and their associated collections. It also offers an optional graphical interface for using MongoDB's features. So it's a good option if you don't want to type text commands to create databases and collections.

Nevertheless, you'll learn how to use both options in this tutorial.

To get started, head over to the MongoDB Tools Download page to download and install the latest version of MongoDB Shell.

To get the desktop app instead, scroll down on the same page. Then open the MongoDB Compass section and click the Download button.

Accessing MongoDB Shell Within MongoDB Compass

If you want to access Mongosh within the GUI desktop app instead of the dedicated shell:

  1. Once installed, open MongoDB Compass.
  2. Click >_MONGOSH at the bottom-left corner of the app to activate the shell.
  3. Drag up the three horizontal bars at the bottom of the app to expand the shell interface.
Mongosh tab in MongoDB compass

How to Create Database and Collections Using MongoDB Shell

MongoDB Shell accepts a series of text commands for communicating with a database.

Related:How to Connect Your Flask App With CouchDB: A NoSQL Database

A database in MongoDB, however, doesn't physically exist without a collection. In essence, the new database is only visible in MongoDB Compass when you insert a collection into it.

Creating a Database

In either MongoDB Compass or the MongoDB Shell app, use the following command to create a database:

        use mynewdatabase
    

MongoDB initiates and selects the new database by default when you run the above command. So it starts operating within the scope of the new database immediately. Unless you switch to another one, any code you run affects this new database.

How to Create Collections in Mongosh

You can think of collections in MongoDB as a group of related tables in SQL.

To make the new database visible and usable, you need to insert at least a collection into it. Here's the command for creating an empty collection in MongoDB Shell:

        db.createCollection(name)

Where name is a string representing the name of the collection to create. For example, to create a collection called MUO in your selected database:

        db.createCollection("MUO")

Related:How to Create Documents in MongoDB

Note: Just as it's possible to create many tables in an SQL database, you can create as many collections as you want in MongoDB.

How to Insert Data Into a MongoDB Database

Like most NoSQL databases, MongoDB inserts data as JSON objects, which are key-value pairs.

To insert data into the previously created collection:

        db.MUO.insertOne({FirstName: "Idowu"})
    

Creating Database and Collections Using MongoDB Compass

If you've already installed it, launch MongoDB Compass on your PC.

Then follow these instructions for creating a database and collections using the GUI option:

  1. Click Connect to load existing databases.
  2. At the top-left corner, click CREATE DATABASE.
    MongoDB Create database
  3. Type in your preferred database name in the Database Name field.
  4. Since MongoDB requires a collection to complete database creation, fill in the Collection Name field as well.
  5. Click Create Database to get a new database and collection.
    MongoDB create database and collection

You can confirm that MongoDB created your database and collections by refreshing the MongoDB Compass view. Do this by clicking the refresh (circular arrow) icon at the top-right corner of the left sidebar.

The app's sidebar also displays a list of databases. Click a database to view the collections within it.

Make the Best Use of MongoDB

Whether you're using it to store your to-do list, or as the backend of a more complex app, MongoDB is straightforward. Of course, besides creating a database and collections, there's still a lot more you can do.

Whether you're using the shell option or MongoDB Compass's GUI, you can add data, run queries, delete data, and update it. These common actions are perfect for getting to grips with a new, NoSQL database engine.