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

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.
- Introduction to MongoDB
- Installation & Database Creation (You are here).
- CRUD Operations
- Embedded Documents and Arrays
- Validation Rules - Enforcing Structure in MongoDB
- Visualize MongoDB Relationships (Embedded vs Referenced)
- What Is an Index in MongoDB?
- 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:

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
- Go to the official MongoDB download page[3] and get the latest version for Windows.
- Run the installer and follow the instructions on screen. Select the option to install MongoDB as a Service.

- Open Command Prompt and type mongosh. If the MongoDB shell opens, the install worked.
Install MongoDB on macOS
- Open Terminal and install MongoDB with Homebrew: brew tap mongodb/brew then brew install mongodb-community
- Start the server: brew services start mongodb/brew/mongodb-community
- Type mongosh to confirm the shell connects.
Install MongoDB on Linux
- On Ubuntu, install the package: sudo apt-get install -y mongodb
- Start the service: sudo service mongodb start
- 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.
- Sign in to MongoDB Atlas and create or open a cluster.
- Add your database user and allow your IP address to connect.
- Open Browse Collections.
- Click Create Database.
- 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:
- Connect to your local or remote MongoDB instance.
- Click Create Database.
- Enter the database name and initial collection name.
- 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 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.
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:
- Right-click on the left side of the Project Structure panel.
- Select Create Database from the menu.
- In the pop-up window, enter the name of your new database. Call it flights2.
- Click OK. The database is created on your MongoDB server.

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

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

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

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.

Insert data into the collection in DbSchema
Documents go in through the Relational Data Editor:
- In the table header, click the first icon, Relational Data Editor. A new panel opens.
- Click the + icon.
- Insert data into the JSON document, which is already structured for you.
- Click Execute & Keep Inserting.

Retrieve data from the collection in DbSchema
Reading the documents back does not need a query either:
- Click on the collection header and select Query Editor.
- DbSchema generates the find query. Click Run.
- The results are shown in a table, like a spreadsheet.

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
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.