0
0
MongoDBquery~30 mins

String and number types in MongoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Add the new field inside the object with a comma after the previous field.