0
0
MongoDBquery~20 mins

MongoDB Shell (mongosh) basics - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB Shell Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find documents with a specific field value
Given a collection users with documents containing age and name, what is the output of this query?

db.users.find({ age: { $gt: 30 } }).toArray()

Assume the collection has:
{ name: 'Alice', age: 25 }
{ name: 'Bob', age: 35 }
{ name: 'Carol', age: 40 }
MongoDB
db.users.find({ age: { $gt: 30 } }).toArray()
A[{ name: 'Alice', age: 25 }, { name: 'Bob', age: 35 }, { name: 'Carol', age: 40 }]
B[{ name: 'Alice', age: 25 }]
C[{ name: 'Bob', age: 35 }, { name: 'Carol', age: 40 }]
D[]
Attempts:
2 left
💡 Hint
Look for documents where age is greater than 30.
📝 Syntax
intermediate
1:30remaining
Identify the syntax error in the query
Which option contains a syntax error when trying to find documents where status is 'active'?
MongoDB
db.orders.find({ status: 'active' })
Adb.orders.find({ status: 'active' })
Bdb.orders.find({ status = 'active' })
Cdb.orders.find({ 'status': 'active' })
Ddb.orders.find({ status: "active" })
Attempts:
2 left
💡 Hint
Check the use of colon and equal sign in object syntax.
optimization
advanced
2:00remaining
Optimize query to return only specific fields
You want to find all documents in products collection but only return the name and price fields. Which query is correct?
Adb.products.find({}, { name: 1, price: 1 })
Bdb.products.find({}, { name: true, price: true })
Cdb.products.find({}, { name: 1, price: 0 })
Ddb.products.find({}, { name: 0, price: 0 })
Attempts:
2 left
💡 Hint
Use projection to include fields by setting them to 1.
🧠 Conceptual
advanced
2:30remaining
Understanding the effect of update operators
What will be the result of this update operation?

db.inventory.updateOne({ item: 'book' }, { $set: { qty: 50 }, $inc: { sold: 5 } })

Assuming the document before update is:
{ item: 'book', qty: 20, sold: 10 }
A{ item: 'book', qty: 50, sold: 15 }
B{ item: 'book', qty: 70, sold: 15 }
C{ item: 'book', qty: 50, sold: 5 }
D{ item: 'book', qty: 20, sold: 15 }
Attempts:
2 left
💡 Hint
Remember $set replaces the field value; $inc adds to the existing value.
🔧 Debug
expert
3:00remaining
Identify the cause of the error in aggregation pipeline
This aggregation pipeline causes an error:

db.sales.aggregate([ { $match: { status: 'A' } }, { $group: { _id: '$cust_id', total: { $sum: '$amount' } } }, { $sort: { total: -1 } } ])

What is the most likely cause of the error?
AThe $match stage is missing a required field
BThe $sort stage must come before $group stage
CField names in $group stage must not start with '$'
DThe $sum operator requires numeric values but 'amount' contains strings
Attempts:
2 left
💡 Hint
Check data types for aggregation operators.