Our biggest-ever sale. 60% off AI & data skills
Buy Now
Yes, you can create a MongoDB database using tools like MongoDB Compass, programmatically via languages like Python or JavaScript, or through APIs like Mongoose.
MongoDB only creates a database when data is inserted—either by creating a collection or inserting a document.
No, MongoDB will automatically create both the database and collection when you insert the first document.
MongoDB will silently create a new database or collection with the incorrect name, which can lead to confusion or data issues.
Yes, database and collection names in MongoDB are case-sensitive, so “Employees” and “employees” are treated as different.
Use the show dbs command after inserting data to see a list of databases, including any newly created ones.
Yes, you can delete a MongoDB database using the db.dropDatabase() command in the shell.
No, the same process works with MongoDB Community or Enterprise editions installed locally.
MongoDB automatically creates a unique _id field for each document, which acts as the primary key unless you specify your own.
Yes, tools like MongoDB Compass and third-party GUIs like Robo 3T allow you to visualize and manage your databases easily.

ผู้เขียน
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.
บล็อก
บล็อก
บทแนะนำ
Learn more about MongoDB with these courses!
คอร์ส
เรียนรู้การจัดการและวิเคราะห์ข้อมูลที่มีโครงสร้างยืดหยุ่นด้วย MongoDB
คอร์ส
ในหลักสูตรเชิงแนวคิดนี้ (ไม่ต้องเขียนโค้ด) คุณจะได้เรียนรู้เกี่ยวกับฐานข้อมูล NoSQL หลัก 4 ประเภทและเอนจินยอดนิยม
คอร์ส
พิชิต NoSQL พร้อมยกระดับเวิร์กโฟลว์ข้อมูล เรียนรู้ Snowflake เพื่อทำงานกับ big data, Postgres JSON เพื่อจัดการข้อมูลเอกสาร และ Redis สำหรับข้อมูลแบบ key-value
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. You can also check out this short video explanation of how to create a database within your MongoDB instance.
After connecting to your cluster, you could execute something like this:
In the above example, "dbinc" is the database we want to use. However, the
usefunction 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:
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:
To change things up, take the following, for example:
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
_idvalue 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.
use(“db_name”)will create a database because it won’t. Remember, you have to create a collection or add data first.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.