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
use databaseName
db.createCollection('collectionName')use databaseName switches to or creates a database.
db.createCollection() makes a new collection inside the current database.
myShop database and creates a products collection.use myShop
db.createCollection('products')school database and creates a students collection.use school
db.createCollection('students')testDB. The database is created when you add data or collections.use testDB
// No collection created yet, but database is readyThis example creates a database called library and a collection called books. Then it lists all collections in the current database.
use library
db.createCollection('books')
show collectionsDatabases 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.
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
use myDatabase do in MongoDB?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]
use switches or creates database [OK]- Thinking
usecreates collections - Confusing database and collection commands
- Assuming
usedeletes databases
users in the current database?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]
db.createCollection('name') to create collections [OK]- Using SQL-like syntax such as 'create collection users'
- Trying to create collection with
db.users.create() - Using
use usersto create collection
use shopDB
db.createCollection('products')
db.getCollectionNames()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]
db.getCollectionNames() [OK]- Expecting database name in collection list
- Assuming collections list is empty after creation
- Confusing collection names with database names
use testDB db.createCollection(users)
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]
createCollection [OK]- Omitting quotes around collection names
- Misusing
usecommand syntax - Thinking collection names cannot be common words
libraryDB and inside it a collection named books. Which sequence of commands will achieve this correctly?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]
- Trying to create database and collection in one command
- Using SQL syntax like 'create database'
- Confusing collection and database names
Ready to prove it?
Pass the test and this pattern moves unknown
