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
Working with String and Number Types in MongoDB
📖 Scenario: You are managing a small bookstore's inventory database using MongoDB. Each book has a title, author, and price. You want to store this information correctly using string and number types.
🎯 Goal: Create a MongoDB collection document for a book with proper string and number types. Then add a discount rate as a number and calculate the discounted price.
📋 What You'll Learn
Create a document with fields title and author as strings
Add a price field as a number
Add a discountRate field as a number representing a percentage
Calculate the discountedPrice by applying the discount rate to the price
💡 Why This Matters
🌍 Real World
Managing product data in a database requires correct use of string and number types to store names, prices, and calculations like discounts.
💼 Career
Database developers and data engineers often work with documents that mix strings and numbers and must perform calculations and updates on stored data.
Progress0 / 4 steps
1
Create the initial book document
Create a MongoDB document called book with these exact fields and values: title set to "The Great Gatsby", author set to "F. Scott Fitzgerald", and price set to 20.99 (a number).
MongoDB
Hint
Use curly braces to create an object. Strings go in quotes, numbers do not.
2
Add a discount rate
Add a new field discountRate to the book document and set it to 15 (a number representing 15%).
MongoDB
Hint
Add the new field inside the object with a comma after the previous field.
3
Calculate the discounted price
Create a new variable called discountedPrice that calculates the price after applying the discount rate. Use the formula: price minus (price times discountRate divided by 100).
MongoDB
Hint
Use arithmetic operators and access the fields with dot notation.
4
Add discounted price to the book document
Add the discountedPrice field to the book document and set its value to the discountedPrice variable you calculated.
MongoDB
Hint
Add the new field inside the object with a comma after the previous field.
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
Step 1: Understand string representation in MongoDB
Strings must be enclosed in quotes to be recognized as text.
Step 2: Identify correct syntax for strings
Double quotes or single quotes can be used, but quotes are necessary around text.
Final Answer:
Use quotes around the text, like "Hello" -> Option D
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
Step 1: Understand number representation in MongoDB
Numbers are stored without quotes to be recognized as numeric values.
Step 2: Identify correct syntax for numbers
Writing numbers without quotes stores them as numeric types, not strings.
Final Answer:
Write it as 42 without quotes -> Option C
Quick Check:
Numbers have no quotes = A [OK]
Hint: Numbers never have quotes in MongoDB documents [OK]