MongoDB Create Database – mongosh, Atlas, Compass, Docker, and DbSchema | DbSchema



MongoDB create database tutorial with shell and GUI methods

To create a MongoDB database, open mongosh[1], run use flights to switch to it, then insert the first document with db.flightData.insertOne(). MongoDB creates the database and the collection at that first write[2]. DbSchema, MongoDB Atlas, Compass and Docker reach the same result through a GUI or a container, and every path is covered below.

This is lesson 2 of the DbSchema MongoDB course. It covers every way to create a MongoDB database, why an empty one does not survive, how to install MongoDB Server when you want the local shell, and how to do the same work visually in DbSchema.

  1. Introduction to MongoDB
  2. Installation & Database Creation (You are here).
  3. CRUD Operations
  4. Embedded Documents and Arrays
  5. Validation Rules - Enforcing Structure in MongoDB
  6. Visualize MongoDB Relationships (Embedded vs Referenced)
  7. What Is an Index in MongoDB?
  8. Aggregation Pipeline Explained

Create a MongoDB database in mongosh

mongosh is the shell that ships with MongoDB Server, and it is where the two commands that actually create a database are run. Whatever operating system you are on, the sequence is identical: switch to the database name you want, then insert a document into it.

Switch to a new database with use

Run show dbs first to list what the server already holds. On a fresh install that is admin, config and local, and nothing else. Switch to the database you want:

 use flights

The prompt now reports flights as the current database and db resolves to it. Nothing has been written to the server yet.

Create the first collection and insert a document

db.flightData.insertOne() creates the flightData collection and the flights database in the same call, because MongoDB creates both on the first write[2]:

 db.flightData.insertOne({
 "departureAirport": "LHR",
 "arrivalAirport": "TXL",
 "aircraft": "Airbus A320",
 "distance": 950,
 "intercontinental": false
})

MongoDB answers with acknowledged: true and the ObjectId it assigned to the new document. Run show dbs again and flights is now in the list.

Verify the database in mongosh

Read the document back to confirm the write landed:

 db.flightData.find().pretty()

The .pretty() method only formats the output; the query works without it, and recent mongosh versions already print documents formatted. The result looks like this:

Retrieve Data Find Pretty

Why an empty MongoDB database does not persist

use flights creates nothing on the server. It sets the current database for the session and no more, which is why show dbs will not list flights until something is stored in it. MongoDB allocates the database and its first collection at the first write[2], and drops a database again once its last collection is gone.

  • Running use on a name that does not exist yet, then show dbs, shows nothing new. That is expected, not a failed command.
  • db.createCollection("flightData") also materialises the database, because a collection is something the server has to store.
  • Dropping the last collection with db.flightData.drop() leaves an empty database, and MongoDB does not keep it.
  • A misspelled name in use silently creates a second database the moment you insert, so check the prompt before the first write.

This is the point where MongoDB parts company with relational engines. A PostgreSQL CREATE DATABASE statement writes a catalog entry immediately and the database exists whether or not it holds a single table, and the same is true in SQL Server and in SQLite, where creating the file is creating the database. In MongoDB the name is only a label until a document gives it something to hold.

Install MongoDB Server

The mongosh path above needs MongoDB Server running locally. Install it once per machine; the Atlas and Docker sections further down skip this step entirely.

Install MongoDB on Windows

  1. Go to the official MongoDB download page[3] and get the latest version for Windows.
  2. Run the installer and follow the instructions on screen. Select the option to install MongoDB as a Service. MongoDB for Windows
  3. Open Command Prompt and type mongosh. If the MongoDB shell opens, the install worked.

Install MongoDB on macOS

  1. Open Terminal and install MongoDB with Homebrew: brew tap mongodb/brew then brew install mongodb-community
  2. Start the server: brew services start mongodb/brew/mongodb-community
  3. Type mongosh to confirm the shell connects.

Install MongoDB on Linux

  1. On Ubuntu, install the package: sudo apt-get install -y mongodb
  2. Start the service: sudo service mongodb start
  3. Type mongosh to confirm the shell connects.

Create a database in MongoDB Atlas

MongoDB Atlas is the hosted option, and a lot of teams start there instead of on a local server. The database is still created by its first collection, the same as in the shell.

  1. Sign in to MongoDB Atlas and create or open a cluster.
  2. Add your database user and allow your IP address to connect.
  3. Open Browse Collections.
  4. Click Create Database.
  5. Enter the database name and the first collection name, then confirm.

Atlas is worth it when you want hosted infrastructure, easy sharing, and a quick way to connect tools like DbSchema or Compass without running the server yourself.

Create a database in MongoDB Compass

Compass is MongoDB's own desktop GUI, and it creates a database in four clicks:

  1. Connect to your local or remote MongoDB instance.
  2. Click Create Database.
  3. Enter the database name and initial collection name.
  4. Confirm, then open the new database and browse the collection list.

Compass fits when you want the official MongoDB GUI for browsing data, checking validation rules, and inspecting aggregations.

Create a database with Docker

Docker suits local development, tutorials and reproducible team setups, because nothing is installed on the host:

docker run --name mongodb-dev -p 27017:27017 -d mongo:8
docker exec -it mongodb-dev mongosh

From inside the container the shell behaves exactly as it does on a local install:

use flights
db.flightData.insertOne({ departureAirport: "LHR" })

Delete the container and the database goes with it, which is what makes this the quickest way to get a disposable MongoDB for testing.

Create a MongoDB database visually in DbSchema

DbSchema Database Designer

DbSchema connects to MongoDB through the JDBC driver it ships, reverse-engineers the databases you tick, and draws each collection as a box of fields. Where a collection carries no schema validation rule[4], DbSchema introspects a configurable sample of documents per collection and infers field names, BSON types, nested objects and arrays. What you see is an approximation of what the sampled documents contain, not a schema MongoDB itself enforces. Where a collection does carry a validation rule, DbSchema reads that rule as the authoritative structure instead, and creating or editing a collection in DbSchema writes that rule back to both the database and the local model file.

MongoDB connection dialog in DbSchema with the bundled JDBC driver and On-Premise mode
Selecting which MongoDB databases and collections DbSchema pulls into the model

That makes it a fast way to understand your database structure without typing commands, and it is the same panel you use to create a new database.

Once you are connected to your MongoDB server with DbSchema, a new database takes four steps and no shell:

  1. Right-click on the left side of the Project Structure panel.
  2. Select Create Database from the menu.
  3. In the pop-up window, enter the name of your new database. Call it flights2.
  4. Click OK. The database is created on your MongoDB server.

Create a Database with DbSchema

Create a collection in DbSchema

  1. Select flights2 on the left so the new collection is created in that database.
  2. Right-click on the main screen and choose Create Validator / Define Collection.

Create a Collection with DbSchema

  1. Name the collection flightData, the same name used in mongosh above, so you can see how the same objects look in a GUI.

Create a Collection with DbSchema

  1. Click Add to add fields to the collection.
  2. Enter the field name _id.
  3. Select the data type ObjectId and check the Mandatory box.
  4. Add the rest of the fields, then click OK.

Create a Field with DbSchema

The structure of the new collection now appears in the diagram. At this stage the collection is empty, and the same diagram is what DbSchema later uses to draw relationships between collections that MongoDB itself does not declare.

Create a Collection with DbSchema

Insert data into the collection in DbSchema

Documents go in through the Relational Data Editor:

  1. In the table header, click the first icon, Relational Data Editor. A new panel opens.
  2. Click the + icon.
  3. Insert data into the JSON document, which is already structured for you.
  4. Click Execute & Keep Inserting.

Insert Data into a Collection with DbSchema

Retrieve data from the collection in DbSchema

Reading the documents back does not need a query either:

  1. Click on the collection header and select Query Editor.
  2. DbSchema generates the find query. Click Run.
  3. The results are shown in a table, like a spreadsheet.

Retrieve Data from a Collection with DbSchema

From here the same model carries on into a MongoDB database diagram, documentation and sample-data generation, without going back to the shell.

Next lesson: CRUD operations

The database exists and holds its first document. The next lesson goes deeper into CRUD operations:

  • Find documents using queries
  • Insert multiple records
  • Update existing data
  • Delete documents

After that the course covers joining collections with $lookup, and you can go back to the first article for an introduction to MongoDB.

FAQ

Does use flights create a MongoDB database immediately?

No. use only switches the session to that name. MongoDB creates the database when you first store data in it[2], so the database appears in show dbs after the first insert or the first collection.

Can I create a MongoDB database from Atlas or Compass?

Yes. Both Atlas and Compass let you create a database and its first collection through the GUI, and both ask for the collection name at the same time because the database needs it.

What is the easiest way to create a MongoDB database for local testing?

Docker, for most developers. It avoids a full local installation and the whole database is reset by deleting the container.

Can I create a MongoDB database visually in DbSchema?

Yes. DbSchema creates the database, defines collections and fields, inserts sample data, and then carries the same model on into visual design and documentation.

Download DbSchema to create your MongoDB databases, collections and fields from the diagram instead of the shell. Connecting, reverse-engineering and the interactive diagram are in the free Community Edition; the Relational Data Editor used above to insert and read documents is a Pro feature, and Pro has a 15-day trial.

Sources

  1. mongosh - the MongoDB Shell
  2. MongoDB Manual - Databases and Collections
  3. Official MongoDB Website
  4. MongoDB Manual - Schema Validation

Create MongoDB databases and collections visually

DbSchema connects to MongoDB, samples your documents to draw each collection as a diagram, and lets you create databases, collections and fields without the shell. Connecting, reverse-engineering and interactive diagrams are in the free Community Edition.

DbSchema Design your database visually - free

DbSchema ER Diagram Download free
Visual Design & Schema Diagram

✓ Create and manage your database schema visually through a user-friendly graphical interface.

✓ Easily arrange tables, columns, and foreign keys to simplify complex database structures, ensuring clarity and accessibility.

GIT & Collaboration
Version Control & Collaboration

✓ Manage schema changes through version control with built-in Git integration, ensuring every update is tracked and backed up.

✓ Collaborate efficiently with your team to maintain data integrity and streamline your workflow for accurate, consistent results.

Data Explorer & Query Builder
Relational Data & Query Builder

✓ Seamlessly navigate and visually explore your database, inspecting tables and their relationships.

✓ Build complex SQL queries using an intuitive drag-and-drop interface, providing instant results for quick, actionable insights.

Interactive Documentation & Reporting
HTML5 Documentation & Reporting

✓ Generate HTML5 documentation that provides an interactive view of your database schema.

✓ Include comments for columns, use tags for better organization, and create visually reports.