Skip to main content
MongoDB
CHAPTER 04 Beginner

MongoDB Create Database & Collection | use command

Updated: May 16, 2026
15 min read

# CHAPTER 4

Creating Databases and Collections

1. Introduction

In traditional SQL databases, creating infrastructure is a tedious, multi-step process. You must write explicit DDL commands (CREATE DATABASE, CREATE TABLE) and rigidly define every column data type before you can save a single piece of data. MongoDB is designed for speed and developer velocity. It uses a concept called Implicit Creation—databases and collections magically create themselves the exact moment you need them. In this chapter, we will open the Mongo Shell and learn how to navigate and construct our digital infrastructure.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Access the mongosh terminal environment.
  • Use the use command to switch or create databases.
  • Understand MongoDB's "Implicit Creation" mechanism.
  • View existing databases and collections.
  • Explicitly create collections with specific options using db.createCollection().

3. The use Command (Creating a Database)

Open your terminal and type mongosh to enter the MongoDB environment.

To see what databases currently exist, type:

javascript
1
show dbs

To create a new database for a school project, you do not type CREATE DATABASE. You simply type:

javascript
1
use school_db

*Output: switched to db schooldb*

The Magic: MongoDB doesn't actually physically create schooldb on the hard drive yet. It just holds it in memory. A MongoDB database is strictly "lazy"—it will not permanently save itself to the disk until you insert at least one Document into it!

4. Implicit Collection Creation

Now that we are "inside" school_db, we need a collection to hold students. Again, you don't *have* to write a creation command. You can just instantly insert a document into a collection that doesn't exist yet!
javascript
12
// This instantly creates the 'students' collection AND inserts the document!
db.students.insertOne({ name: "John Doe", age: 20 })

*(If you type show collections, you will now see students permanently exists!)*

5. Explicit Collection Creation

While Implicit Creation is great for prototyping, in enterprise production environments, you often want to create a collection manually to apply specific backend rules (like size limits or validation schemas).

You do this using the explicit db.createCollection() command.

javascript
12
// Manually create a collection for teachers
db.createCollection("teachers")

6. Creating "Capped" Collections

One of the best uses of explicit creation is creating a Capped Collection. A capped collection has a fixed maximum size. Once it gets full, it acts like a circular queue: inserting a new document automatically overwrites the absolute oldest document. This is brilliant for "Log Files" where you only care about the last 10,000 events!
javascript
123
// Create a collection that can only hold a maximum of 1,000,000 bytes.
// Once full, the oldest logs are automatically deleted!
db.createCollection("server_logs", { capped: true, size: 1000000 })

7. Verifying Your Infrastructure

To navigate your server and see what you have built:
javascript
12345678
// Show all databases on the server
show dbs

// Show what database you are currently 'using'
db

// Show all collections inside the current database
show collections

8. Mini Project: Student Management Database

Let's build the scaffolding for a university app.
javascript
123456789101112
// 1. Switch to a new database (Implicit creation)
use university_app

// 2. Explicitly create the core collections
db.createCollection("students")
db.createCollection("courses")

// 3. Create a capped collection for recent system alerts
db.createCollection("system_alerts", { capped: true, size: 5000000 })

// 4. Verify
show collections

9. Common Mistakes

  • Typo Database Creation: Because of implicit creation, if you mean to switch to universitydb but accidentally type use unversitydb (missing the 'i'), MongoDB will happily create a brand new, empty database with the typo name! Always double-check your spelling.
  • Empty Databases disappearing: If you type use secretdb, but never insert any data, and then restart the MongoDB server... secretdb will vanish. Databases must contain data to persist.

10. Best Practices

  • Naming Conventions: Database and Collection names should be lowercase, plural, and use underscores for spaces (snakecase).
*Good:* userprofiles, ecommercedb. *Bad:* UserProfiles, my cool database.
  • Avoid System Names: Never name a database admin, local, or config. These are reserved by MongoDB for internal server routing.

11. Exercises

  1. 1. What command do you use to switch to a database named inventorydb?
  1. 2. If you type show dbs immediately after typing use ghostdb, why will ghostdb not appear in the list?

12. MongoDB Challenges

Write the explicit command to create a collection named notifications that is "capped" to a maximum of 5,000,000 bytes.
javascript
1
db.createCollection("notifications", { capped: true, size: 5000000 })

13. MCQ Quiz with Answers

Question 1

What happens in MongoDB if you execute an insertOne() command on a collection name that does not currently exist?

Question 2

What is the unique defining feature of a "Capped Collection" in MongoDB?

14. Interview Questions

  • Q: Explain MongoDB's concept of "Implicit Creation" for databases and collections. What are the developmental benefits and potential risks of this feature?
  • Q: Describe a real-world software architecture scenario where utilizing a MongoDB Capped Collection is vastly superior to a standard collection.

15. FAQs

Q: Can I drop (delete) a collection? A: Yes! If you made a mistake, ensure you are in the correct database and type db.collectionName.drop(). Be careful, this permanently destroys all data inside it!

16. Summary

MongoDB removes architectural friction. By utilizing the use command and embracing implicit creation, you can prototype databases at the speed of thought. By understanding explicit creation, you can deploy advanced features like capped collections for high-performance logging queues.

17. Next Chapter Recommendation

We have our empty buckets ready. Before we start dumping data into them, we need to understand exactly what *kind* of data MongoDB accepts. In Chapter 5: MongoDB Data Types Explained, we will explore Strings, Booleans, and the legendary ObjectId.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·