Skip to main content

How to Create a Database in MongoDB: A Quick Guide

Discover how to create a MongoDB database from the shell or with a script, plus common pitfalls to avoid.
Jul 1, 2025  · 3 min read

So, you've identified MongoDB as the best database choice for your application, but aren't quite sure how to get your database up and running. You've come to the right place, and the good news is that it takes less than a minute.

The assumption here is that you already have a MongoDB Atlas, MongoDB Community Edition, or MongoDB Enterprise Edition cluster deployed.

For the sake of this example, the MongoDB Shell will be used, but everything will translate to other tools and programming languages with minimal differences.

After connecting to your cluster, you could execute something like this:

use("dbinc");

In the above example, "dbinc" is the database we want to use. However, the use function only changes the context. It doesn't actually create a database. 

Unlike other database tools, MongoDB only creates a database when storing data. This could be when you create a collection or your first document.

Take the following, for example:

use("dbinc");

db.createCollection("employees");

The above commands would create a "dbinc" database with an "employees" collection. It should be called out though that you don't need to explicitly create a collection prior to creating documents. 

You can verify that the database was created by executing the following command:

show dbs

To change things up, take the following, for example:

use("dbinc");

db.employees.insertMany([
    { "name": "Nic Raboy", "title": "Master of Shenanigans", "salary": 50000, "last_raise_date": new Date("2013-06-01") },
    { "name": "Megan Grant", "title": "Word Wrangler", "salary": 125000, "last_raise_date": new Date("2020-12-01") },
    { "name": "Tony Kim", "title": "Alliance Czar", "salary": 80000, "last_raise_date": new Date("2025-02-01") },
]);

In the above example, "dbinc" is our database and "employees" is our collection, but if neither exists, both will be created when we insert our data. If either of the two already exists, that is fine too. We'll just be adding our new documents to them.

While out of the scope of this particular article, it is worth noting that MongoDB will create an _id value for you if one isn’t specified. There’s no need to worry about primary key definitions.

There are a few common pitfalls that you should look out for when working with databases in MongoDB.

  • Don’t assume that use(“db_name”) will create a database because it won’t. Remember, you have to create a collection or add data first.
  • If you have a typo in your database name or collection name, you won’t get a warning. Instead, a new database or collection might be created, just like in the previous example.
  • Pay attention to the case sensitivity in the database and collection name.

If you avoid the common pitfalls above, creating and working with databases in MongoDB should be a breeze.

Conclusion

Creating databases in MongoDB is not a difficult task and is not something you need to worry about prior to working with your data, since databases in MongoDB are only created when collections—or in most cases, documents—are created.

Need a more thorough example with more tooling? Get all the details about how to create a MongoDB database in the MongoDB blog.

If you’re in data science or a similar role, check out the Introduction to MongoDB in Python course.

FAQs

Can you create a MongoDB database without using the shell?

Yes, you can create a MongoDB database using tools like MongoDB Compass, programmatically via languages like Python or JavaScript, or through APIs like Mongoose.

When does MongoDB actually create a database?

MongoDB only creates a database when data is inserted—either by creating a collection or inserting a document.

Is it necessary to explicitly create a collection before inserting documents?

No, MongoDB will automatically create both the database and collection when you insert the first document.

What happens if I mistype a database or collection name in MongoDB?

MongoDB will silently create a new database or collection with the incorrect name, which can lead to confusion or data issues.

Is MongoDB case-sensitive when it comes to database and collection names?

Yes, database and collection names in MongoDB are case-sensitive, so “Employees” and “employees” are treated as different.

How can I check if my MongoDB database was successfully created?

Use the show dbs command after inserting data to see a list of databases, including any newly created ones.

Can you delete a MongoDB database once it’s created?

Yes, you can delete a MongoDB database using the db.dropDatabase() command in the shell.

Do you need a MongoDB Atlas account to follow these steps?

No, the same process works with MongoDB Community or Enterprise editions installed locally.

What is the role of the _id field in MongoDB documents?

MongoDB automatically creates a unique _id field for each document, which acts as the primary key unless you specify your own.

Are there any tools for visualizing MongoDB databases?

Yes, tools like MongoDB Compass and third-party GUIs like Robo 3T allow you to visualize and manage your databases easily.


Nic Raboy's photo
Author
Nic Raboy

Nic Raboy is a Developer Relations Lead at MongoDB where he leads a team of Python, Java, C#, and PHP developers who create awesome content to help developers be successful at including MongoDB in their projects. He has experience with Golang and JavaScript and often writes about many of his development adventures.

Topics

Learn more about MongoDB with these courses!

Course

Introduction to MongoDB in Python

4 hr
21.8K
Learn to manipulate and analyze flexibly structured data with MongoDB.
See DetailsRight Arrow
Start Course
See MoreRight Arrow
Related

blog

What Is MongoDB? Key Concepts, Use Cases, and Best Practices

This guide explains MongoDB, how it works, why developers love it, and how to start using this flexible NoSQL database.
Karen Zhang's photo

Karen Zhang

15 min

Tutorial

Creating Collections in MongoDB: Manual and Automatic Methods

Learn how collections are created in MongoDB, when to define them explicitly, and key configuration options.
Luce Carter's photo

Luce Carter

5 min

Tutorial

MongoDB find(): A Complete Beginner's Guide to Querying Data

This guide explains how to use the MongoDB find() method to query, filter, sort, and paginate data with real-world examples. Perfect for beginners and those transitioning from SQL.
Samuel Molling's photo

Samuel Molling

9 min

Tutorial

A Comprehensive NoSQL Tutorial Using MongoDB

Learn about NoSQL databases - why NoSQL, how they differ from relational databases, the different types, and design your own NoSQL database using MongoDB.
Arunn Thevapalan's photo

Arunn Thevapalan

9 min

Tutorial

Introduction to MongoDB and Python

In this tutorial, you'll learn how to integrate MongoDB with your Python applications.
Derrick Mwiti's photo

Derrick Mwiti

12 min

Tutorial

MongoDB Tutorial: How to Set Up and Query MongoDB Databases

Learn how to connect and analyze MongoDB databases in Python.
Bex Tuychiev's photo

Bex Tuychiev

10 min

See MoreSee More