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
Building a Simple MongoDB Document Model
📖 Scenario: You are creating a small database for a local bookstore. The store wants to keep track of books with details like title, author, and price.
🎯 Goal: Build a MongoDB document model using JSON/BSON format to represent books in the bookstore.
📋 What You'll Learn
Create a document representing a book with specific fields
Add a configuration field for currency
Use a query to find books cheaper than a certain price
Complete the document with an additional field for stock quantity
💡 Why This Matters
🌍 Real World
Document models like this are used in MongoDB to store and organize data for applications such as online stores, libraries, and inventory systems.
💼 Career
Understanding how to structure documents and write queries is essential for roles like database administrators, backend developers, and data engineers working with NoSQL databases.
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 10.99.
MongoDB
Hint
Use curly braces to create a document and include the fields exactly as shown.
2
Add a currency field
Add a field called currency to the book document and set it to "USD".
MongoDB
Hint
Add the new field inside the existing document with a comma separating it from previous fields.
3
Query for books cheaper than a price
Write a MongoDB query called cheap_books_query that finds books with price less than 15.
MongoDB
Hint
Use MongoDB's query operator $lt to find prices less than 15.
4
Add stock quantity to the book document
Add a field called stock to the book document and set it to 30.
MongoDB
Hint
Add the stock field with a comma after the previous field inside the document.
Practice
(1/5)
1. Which of the following best describes a MongoDB document?
easy
A. A compiled program file
B. A table with rows and columns like in SQL
C. A set of key-value pairs similar to a JSON object
D. A flat file storing plain text data
Solution
Step 1: Understand MongoDB document structure
MongoDB stores data as documents, which are collections of key-value pairs similar to JSON objects.
Step 2: Compare with other data formats
Unlike tables or flat files, documents can store nested data and arrays, making them flexible and structured.
Final Answer:
A set of key-value pairs similar to a JSON object -> Option C
Quick Check:
Document = JSON-like key-value pairs [OK]
Hint: Think JSON object when you hear MongoDB document [OK]
Common Mistakes:
Confusing documents with SQL tables
Thinking documents are flat text files
Assuming documents are executable files
2. Which of the following is the correct way to represent a nested document in MongoDB?
B. { "name": "Alice", "address": "city: NY, zip: 10001" }
C. { "name": "Alice", "address": ["city", "NY", "zip", 10001] }
D. { "name": "Alice", "address": ("city": "NY", "zip": 10001) }
Solution
Step 1: Identify correct JSON syntax for nested documents
Nested documents are represented as objects inside another object using curly braces {} with key-value pairs.
Step 2: Check each option's syntax
{ "name": "Alice", "address": { "city": "NY", "zip": 10001 } } uses proper JSON syntax with nested braces. The other options use incorrect formats like strings, arrays, or parentheses.