0
0
MongoDBquery~20 mins

Database and collection creation in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB Database Creator
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this MongoDB command?
Consider the following MongoDB shell command:

use myNewDB

What will be the output after running this command?
MongoDB
use myNewDB
A"database myNewDB dropped"
B"error: database does not exist"
C"switched to db myNewDB"
D"created database myNewDB"
Attempts:
2 left
💡 Hint
The 'use' command switches the current database context.
query_result
intermediate
2:00remaining
What happens when you create a collection explicitly?
Given the command:

db.createCollection('users')

What is the result of running this command in MongoDB shell?
MongoDB
db.createCollection('users')
A{"ok": 1}
B{"ok": 0}
Cnull
DError: collection already exists
Attempts:
2 left
💡 Hint
Creating a collection explicitly returns a success status.
📝 Syntax
advanced
2:00remaining
Which command correctly creates a capped collection named 'logs'?
You want to create a capped collection named 'logs' with a maximum size of 1MB. Which command is correct?
Adb.createCollection('logs', {size: 1048576, capped: false})
Bdb.createCollection('logs', {capped: true, size: 1048576})
Cdb.createCollection('logs', {capped: true, max: 1048576})
Ddb.createCollection('logs', {maxSize: 1048576, capped: true})
Attempts:
2 left
💡 Hint
The 'capped' option must be true and 'size' specifies the max bytes.
🧠 Conceptual
advanced
2:00remaining
Why does MongoDB create a database only after inserting data?
In MongoDB, when you run use newDB, the database is not created immediately. Why is this behavior designed this way?
ABecause databases are created automatically when the server starts.
BBecause the 'use' command is only for switching databases and cannot create them.
CBecause MongoDB requires a restart to create new databases.
DBecause MongoDB creates databases lazily only when data is stored to save disk space.
Attempts:
2 left
💡 Hint
Think about resource usage and efficiency.
🔧 Debug
expert
2:00remaining
Identify the error in this collection creation command
What error will this command produce?

db.createCollection('events', {capped: true, size: '1000'})

Note: size is given as a string.
MongoDB
db.createCollection('events', {capped: true, size: '1000'})
AMongoServerError: capped collections require size in bytes
BSyntaxError: unexpected string
CNo error, collection created successfully
DTypeError: size must be a number
Attempts:
2 left
💡 Hint
Check the data type required for the size option.