0
0
MongoDBquery~20 mins

String and number types in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String and Number Types Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find documents with numeric string values
Given a collection products where the field price is stored as a string, which query returns all documents where price is a string representing a number greater than 100?
MongoDB
db.products.find({ price: { $gt: "100" } })
Adb.products.find({ price: { $gte: "100" } })
Bdb.products.find({ price: { $gt: "100" } })
Cdb.products.find({ price: { $gte: 100 } })
Ddb.products.find({ price: { $gt: 100 } })
Attempts:
2 left
💡 Hint
Remember that comparing strings with $gt compares lexicographically.
📝 Syntax
intermediate
2:00remaining
Identify the correct syntax for storing a number as a string
Which of the following MongoDB insert commands correctly stores the field age as a string with value "30"?
Adb.users.insertOne({ name: "Alice", age: Number("30") })
Bdb.users.insertOne({ name: "Alice", age: 30 })
Cdb.users.insertOne({ name: "Alice", age: "30" })
Ddb.users.insertOne({ name: "Alice", age: String(30) })
Attempts:
2 left
💡 Hint
Strings must be enclosed in quotes.
🧠 Conceptual
advanced
2:00remaining
Understanding BSON types for numbers and strings
In MongoDB, which BSON type is used to store a 64-bit integer number?
AInt64 (Long)
BDouble
CString
DDecimal128
Attempts:
2 left
💡 Hint
Think about the size and precision of the number type.
🔧 Debug
advanced
2:00remaining
Why does this query not return expected numeric results?
Consider the collection sales where amount is stored as a string. The query db.sales.find({ amount: { $gt: 100 } }) returns no documents. Why?
MongoDB
db.sales.find({ amount: { $gt: 100 } })
ABecause <code>amount</code> is a string, comparing with number 100 fails to match any document.
BBecause <code>$gt</code> operator only works with numbers, not strings.
CBecause the query syntax is invalid and causes an error.
DBecause the collection <code>sales</code> is empty.
Attempts:
2 left
💡 Hint
Check the data type of the field and the query value.
optimization
expert
2:00remaining
Optimizing queries on numeric strings
You have a collection orders where the field quantity is stored as a string. You want to efficiently find all orders with quantity greater than 50. Which approach is best?
AUse a regex query to match quantities greater than 50 as strings.
BConvert <code>quantity</code> to number in each query using aggregation and filter after conversion.
CUse <code>db.orders.find({ quantity: { $gt: "50" } })</code> and create an index on <code>quantity</code>.
DUpdate the collection to store <code>quantity</code> as a number and create an index on it.
Attempts:
2 left
💡 Hint
Think about data types and indexing for performance.