Creating Databases and Collections
# 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
mongoshterminal environment.
-
Use the
usecommand 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:
To create a new database for a school project, you do not type CREATE DATABASE. You simply type:
*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!
*(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.
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!7. Verifying Your Infrastructure
To navigate your server and see what you have built:8. Mini Project: Student Management Database
Let's build the scaffolding for a university app.9. Common Mistakes
-
Typo Database Creation: Because of implicit creation, if you mean to switch to
universitydbbut accidentally typeuse 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...secretdbwill 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).
userprofiles, ecommercedb.
*Bad:* UserProfiles, my cool database.
-
Avoid System Names: Never name a database
admin,local, orconfig. These are reserved by MongoDB for internal server routing.
11. Exercises
-
1.
What command do you use to switch to a database named
inventorydb?
-
2.
If you type
show dbsimmediately after typinguse ghostdb, why willghostdbnot appear in the list?
12. MongoDB Challenges
Write the explicit command to create a collection namednotifications that is "capped" to a maximum of 5,000,000 bytes.
13. MCQ Quiz with Answers
What happens in MongoDB if you execute an insertOne() command on a collection name that does not currently exist?
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 typedb.collectionName.drop(). Be careful, this permanently destroys all data inside it!
16. Summary
MongoDB removes architectural friction. By utilizing theuse 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 legendaryObjectId.