We create databases and collections to organize and store data in MongoDB. This helps keep data neat and easy to find.
Database and collection creation in MongoDB
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
MongoDB
use databaseName
db.createCollection('collectionName')use databaseName switches to or creates a database.
db.createCollection() makes a new collection inside the current database.
Examples
myShop database and creates a products collection.MongoDB
use myShop
db.createCollection('products')school database and creates a students collection.MongoDB
use school
db.createCollection('students')testDB. The database is created when you add data or collections.MongoDB
use testDB
// No collection created yet, but database is readySample Program
This example creates a database called library and a collection called books. Then it lists all collections in the current database.
MongoDB
use library
db.createCollection('books')
show collectionsImportant Notes
Databases and collections are created lazily in MongoDB. They appear only after you insert data or explicitly create collections.
You don't have to create collections manually; MongoDB creates them automatically when you insert documents.
Summary
Use use databaseName to switch or create a database.
Use db.createCollection('collectionName') to make a new collection.
Collections organize your data inside databases.
Practice
1. What does the command
use myDatabase do in MongoDB?easy
Solution
Step 1: Understand the
Theusecommandusecommand in MongoDB switches the current database context to the specified database.Step 2: Behavior when database does not exist
If the specified database does not exist, MongoDB creates it when you first store data in it.Final Answer:
It switches to the database named 'myDatabase' or creates it if it doesn't exist. -> Option CQuick Check:
use databaseNameswitches or creates database [OK]
Hint: Remember:
use switches or creates database [OK]Common Mistakes:
- Thinking
usecreates collections - Confusing database and collection commands
- Assuming
usedeletes databases
2. Which of the following is the correct syntax to create a new collection named
users in the current database?easy
Solution
Step 1: Identify the MongoDB command for collection creation
The correct MongoDB command to create a collection isdb.createCollection('collectionName').Step 2: Match the syntax with the options
db.createCollection('users') matches the correct syntax exactly for creating a collection named 'users'.Final Answer:
db.createCollection('users') -> Option AQuick Check:
Usedb.createCollection('name')to create collections [OK]
Hint: Use
db.createCollection('name') to create collections [OK]Common Mistakes:
- Using SQL-like syntax such as 'create collection users'
- Trying to create collection with
db.users.create() - Using
use usersto create collection
3. What will be the result of running these commands in order?
use shopDB
db.createCollection('products')
db.getCollectionNames()medium
Solution
Step 1: Switch to or create the database 'shopDB'
Theuse shopDBcommand switches to the database named 'shopDB'. If it doesn't exist, it will be created when data is added.Step 2: Create a collection named 'products'
The commanddb.createCollection('products')creates a new collection called 'products' inside 'shopDB'.Step 3: List collections in the current database
db.getCollectionNames()returns an array of collection names in the current database. Since 'products' was created, it returns ['products'].Final Answer:
['products'] -> Option DQuick Check:
Created collection appears indb.getCollectionNames()[OK]
Hint: Created collections appear in
db.getCollectionNames() [OK]Common Mistakes:
- Expecting database name in collection list
- Assuming collections list is empty after creation
- Confusing collection names with database names
4. Identify the error in this MongoDB command sequence:
use testDB db.createCollection(users)
medium
Solution
Step 1: Check syntax of
The collection name must be a string, so it requires quotes around it.createCollectioncommandStep 2: Identify the error in the command
The commanddb.createCollection(users)is missing quotes around 'users', causing a syntax error.Final Answer:
Missing quotes around collection name 'users'. -> Option AQuick Check:
Collection names must be strings in quotes [OK]
Hint: Always put collection names in quotes in
createCollection [OK]Common Mistakes:
- Omitting quotes around collection names
- Misusing
usecommand syntax - Thinking collection names cannot be common words
5. You want to create a new database called
libraryDB and inside it a collection named books. Which sequence of commands will achieve this correctly?hard
Solution
Step 1: Switch to or create the database 'libraryDB'
The commanduse libraryDBswitches to the database named 'libraryDB'. If it doesn't exist, it will be created when data is added.Step 2: Create the collection 'books' inside 'libraryDB'
Usingdb.createCollection('books')creates the collection named 'books' in the current database context.Final Answer:
use libraryDB db.createCollection('books') -> Option BQuick Check:
Switch database then create collection [OK]
Hint: Switch database first, then create collection inside it [OK]
Common Mistakes:
- Trying to create database and collection in one command
- Using SQL syntax like 'create database'
- Confusing collection and database names
