0
0
MongoDBquery~20 mins

Anti-patterns to avoid in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB Anti-pattern Master
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 query?
Consider a collection users with documents containing name and age. What will this query return?
MongoDB
db.users.find({age: {$gt: 30}}).limit(2)
ASyntax error due to incorrect query
BReturns all users with age greater than 30
CReturns 2 users regardless of age
DReturns the first 2 users with age greater than 30
Attempts:
2 left
💡 Hint
Remember that limit() restricts the number of documents returned.
🧠 Conceptual
intermediate
2:00remaining
Why is embedding large arrays in documents an anti-pattern?
In MongoDB, what is a major problem with embedding very large arrays inside a single document?
AIt improves query speed by avoiding joins
BIt can cause the document size to exceed the 16MB limit, leading to errors
CIt automatically shards the data across servers
DIt compresses the data to save space
Attempts:
2 left
💡 Hint
Think about MongoDB's maximum document size.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in MongoDB aggregation?
Identify the aggregation pipeline stage that will cause a syntax error.
MongoDB
db.orders.aggregate([
  {$match: {status: 'shipped'}},
  {$group: {_id: '$customerId', total: {$sum: '$amount'}}},
  {$sort: {total: -1}}
])
A{$group: {_id: '$customerId', total: {$sum: 'amount'}}}
B{$group: {_id: '$customerId', total: {$sum: '$amount'}}}
C{$sort: {total: -1}}
D{$match: {status: 'shipped'}}
Attempts:
2 left
💡 Hint
Check the syntax of the $sum operator argument.
optimization
advanced
2:00remaining
Which indexing strategy avoids the anti-pattern of slow queries?
Given a collection with frequent queries filtering by category and sorting by price, which index is best to optimize performance?
ACreate a compound index on <code>category</code> and <code>price</code>
BCreate a single-field index on <code>price</code>
CCreate a single-field index on <code>category</code>
DNo index needed; rely on collection scan
Attempts:
2 left
💡 Hint
Think about queries that filter and sort on multiple fields.
🔧 Debug
expert
3:00remaining
Why does this MongoDB update not modify any documents?
Given the collection products and this update command, why are no documents updated?
MongoDB
db.products.updateMany({tags: 'sale'}, {$set: {discount: 10}})
AThe <code>$set</code> operator is used incorrectly
BThe update command syntax is invalid
CThe <code>tags</code> field is an array, so matching a string directly fails
DThe <code>updateMany</code> method requires a third argument
Attempts:
2 left
💡 Hint
Consider how MongoDB matches array fields in queries.