Bird
Raised Fist0
MongoDBquery~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. Which of the following is the correct way to store a string value in a MongoDB document?
easy
A. Use single quotes only, like 'Hello'
B. Write the text without quotes, like Hello
C. Write the text as a number, like 123
D. Use quotes around the text, like "Hello"

Solution

  1. Step 1: Understand string representation in MongoDB

    Strings must be enclosed in quotes to be recognized as text.
  2. Step 2: Identify correct syntax for strings

    Double quotes or single quotes can be used, but quotes are necessary around text.
  3. Final Answer:

    Use quotes around the text, like "Hello" -> Option D
  4. Quick Check:

    Strings need quotes = C [OK]
Hint: Strings always need quotes around text in MongoDB [OK]
Common Mistakes:
  • Writing text without quotes causes errors
  • Confusing numbers with strings
  • Using numbers when text is needed
2. Which of the following is the correct way to store the number 42 in a MongoDB document?
easy
A. Write it as '42' with single quotes
B. Write it as "42" with quotes
C. Write it as 42 without quotes
D. Write it as forty-two without quotes

Solution

  1. Step 1: Understand number representation in MongoDB

    Numbers are stored without quotes to be recognized as numeric values.
  2. Step 2: Identify correct syntax for numbers

    Writing numbers without quotes stores them as numeric types, not strings.
  3. Final Answer:

    Write it as 42 without quotes -> Option C
  4. Quick Check:

    Numbers have no quotes = A [OK]
Hint: Numbers never have quotes in MongoDB documents [OK]
Common Mistakes:
  • Putting numbers inside quotes makes them strings
  • Using words instead of digits for numbers
  • Mixing string and number types
3. What will be the output of this MongoDB query?
db.products.find({ price: 100 })

Assuming the collection has documents:
{ "name": "Pen", "price": 100 }
{ "name": "Book", "price": "100" }
medium
A. Returns only the document where price is number 100
B. Returns both documents because price matches 100
C. Returns only the document where price is string "100"
D. Returns no documents because of type mismatch

Solution

  1. Step 1: Understand MongoDB type matching in queries

    MongoDB matches both value and type when querying, so number 100 matches only number 100.
  2. Step 2: Analyze the documents

    Document with price as number 100 matches; document with price as string "100" does not.
  3. Final Answer:

    Returns only the document where price is number 100 -> Option A
  4. Quick Check:

    Query matches value and type = B [OK]
Hint: MongoDB matches type and value in queries [OK]
Common Mistakes:
  • Assuming string and number values match
  • Ignoring type differences in queries
  • Expecting all similar values to match
4. You wrote this MongoDB insert command:
db.users.insertOne({ "age": "30" })

But you want age stored as a number. What is wrong?
medium
A. The insertOne command syntax is incorrect
B. Age is stored as a string because of quotes around 30
C. The field name should be "Age" with capital A
D. Age is stored as a number but should be a string

Solution

  1. Step 1: Check the value type in the insert command

    Quotes around 30 make it a string, not a number.
  2. Step 2: Understand how to store numbers

    To store as number, write 30 without quotes.
  3. Final Answer:

    Age is stored as a string because of quotes around 30 -> Option B
  4. Quick Check:

    Quotes make value string = D [OK]
Hint: Remove quotes to store numbers, add quotes for strings [OK]
Common Mistakes:
  • Thinking quotes don't affect type
  • Changing field name unnecessarily
  • Assuming syntax error without checking value type
5. You have a collection with documents:
{ "item": "apple", "quantity": "10" }
{ "item": "banana", "quantity": 10 }

You want to find all documents where quantity is greater than 5. Which query works correctly?
hard
A. db.collection.find({ quantity: { $gt: NumberInt(5) } })
B. db.collection.find({ quantity: { $gt: "5" } })
C. db.collection.find({ quantity: { $gt: 5 } })
D. db.collection.find({ quantity: { $gt: 5 } }).toArray()

Solution

  1. Step 1: Understand type comparison in MongoDB queries

    MongoDB compares values and types; string "10" is not greater than number 5.
  2. Step 2: Use NumberInt to ensure numeric comparison

    Using NumberInt(5) ensures the query compares numbers, matching numeric quantity fields.
  3. Final Answer:

    db.collection.find({ quantity: { $gt: NumberInt(5) } }) -> Option A
  4. Quick Check:

    Use numeric type for numeric comparison = A [OK]
Hint: Use numeric types in queries to compare numbers correctly [OK]
Common Mistakes:
  • Comparing strings with numbers directly
  • Using quotes around numbers in queries
  • Assuming all quantity fields are same type