MongoDB Create Database & Collection

Databases and collections are created on first use.

✍️ 2 exercises in this lesson

You don't need a CREATE command: use school switches to a database, and it is created when you first store data in it.

Collections are also created automatically on the first insert. You can create one explicitly (for example with validation rules) with db.createCollection("students").

Key Takeaways: MongoDB Create Database & Collection

Mastering MongoDB Create Database & Collection helps you write clean and reliable MongoDB code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

MongoDB Create Database & Collection Code Example

MongoDB
use school
db.createCollection("students")
show collections
Output
switched to db school
{ ok: 1 }
students
Run this example on your own computer. The "Get Started" lesson shows how to set it up.

MongoDB Create Database & Collection Practice Exercises

Check what you learned in “MongoDB Create Database & Collection”.

/ 2 solved
Fill in the blank

Create a collection explicitly.

db.("orders")
✓ Correct! Not quite. Try again.
Multiple choice

When is a MongoDB database actually created?

✓ Correct! Not quite. Try again.
to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services