Bird
Raised Fist0
MongoDBquery~20 mins

Document model mental model (JSON/BSON) 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
🎖️
Document Model Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this MongoDB find query?

Consider a collection users with documents like:

{ "name": "Alice", "age": 30, "hobbies": ["reading", "hiking"] }

What will this query return?

db.users.find({ "hobbies": "hiking" })
MongoDB
db.users.find({ "hobbies": "hiking" })
A[{ "name": "Alice", "age": 30, "hobbies": ["reading", "hiking"] }]
B[]
C[{ "name": "Alice", "age": 30 }]
D[{ "hobbies": "hiking" }]
Attempts:
2 left
💡 Hint

Think about how MongoDB matches array fields with a value.

🧠 Conceptual
intermediate
1:30remaining
Which BSON data type is used to store a date?

In MongoDB's BSON format, which data type stores date and time values?

ATimestamp
BObjectId
CDate
DString
Attempts:
2 left
💡 Hint

It is a special BSON type different from a string.

📝 Syntax
advanced
2:00remaining
Which option correctly inserts a nested document in MongoDB?

You want to insert a document with a nested address field. Which command is correct?

MongoDB
db.customers.insertOne({ name: "John", address: { city: "NY", zip: "10001" } })
Adb.customers.insertOne({ name: "John", address: { city: "NY", zip: "10001" } })
Bdb.customers.insertOne({ name: "John", address: [ city: "NY", zip: "10001" ] })
Cdb.customers.insertOne({ name: "John", address: ( city: "NY", zip: "10001" ) })
Ddb.customers.insertOne({ name: "John", address: "city: NY, zip: 10001" })
Attempts:
2 left
💡 Hint

Remember nested documents use curly braces, not brackets or parentheses.

optimization
advanced
2:30remaining
Which index type improves search on nested fields in MongoDB?

You want to speed up queries filtering by a nested field address.city. Which index type should you create?

ACreate a multikey index on <code>address.city</code>
BCreate a compound index on <code>address.city</code> and <code>address.zip</code>
CCreate a single field index on <code>address</code>
DCreate a single field index on <code>address.city</code>
Attempts:
2 left
💡 Hint

Think about indexing a specific nested field.

🔧 Debug
expert
3:00remaining
Why does this MongoDB query return no results?

Given documents with a field tags as an array of strings, why does this query return no documents?

db.posts.find({ tags: { $eq: ["mongodb", "database"] } })
ABecause $eq does not match arrays, only single values
BBecause the query looks for exact array match, but documents have tags in different order
CBecause $eq requires a nested query with $all for arrays
DBecause the field name is incorrect; it should be 'tag' not 'tags'
Attempts:
2 left
💡 Hint

Think about how MongoDB compares arrays in queries.

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

  1. Step 1: Understand MongoDB document structure

    MongoDB stores data as documents, which are collections of key-value pairs similar to JSON objects.
  2. Step 2: Compare with other data formats

    Unlike tables or flat files, documents can store nested data and arrays, making them flexible and structured.
  3. Final Answer:

    A set of key-value pairs similar to a JSON object -> Option C
  4. 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?
easy
A. { "name": "Alice", "address": { "city": "NY", "zip": 10001 } }
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

  1. 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.
  2. 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.
  3. Final Answer:

    { "name": "Alice", "address": { "city": "NY", "zip": 10001 } } -> Option A
  4. Quick Check:

    Nested document = object inside object with braces [OK]
Hint: Nested documents use curly braces inside braces [OK]
Common Mistakes:
  • Using strings instead of nested objects
  • Using arrays for nested key-value pairs
  • Using parentheses instead of braces
3. Given the document { "name": "Bob", "scores": [85, 90, 78] }, what is the value of the scores field?
medium
A. "85, 90, 78"
B. [85, 90, 78]
C. {85: true, 90: true, 78: true}
D. 85

Solution

  1. Step 1: Identify the data type of the scores field

    The scores field contains square brackets [], which represent an array in JSON/BSON.
  2. Step 2: Understand array representation

    The array holds the numbers 85, 90, and 78 as elements, so the value is the list [85, 90, 78].
  3. Final Answer:

    [85, 90, 78] -> Option B
  4. Quick Check:

    Square brackets = array = [85, 90, 78] [OK]
Hint: Square brackets mean array, not string or object [OK]
Common Mistakes:
  • Confusing array with string
  • Thinking array is a key-value object
  • Selecting only one element instead of full array
4. Identify the error in this MongoDB document: { "title": "Book", "pages": "300", "author": { "name": "John", "age": 45 }
medium
A. Missing closing brace for the document
B. Pages field should be a number, not a string
C. Title field cannot be a string
D. Author name must be an array

Solution

  1. Step 1: Check document syntax carefully

    The document starts with { but does not have a matching closing brace } at the end.
  2. Step 2: Validate other fields

    While pages is a string, MongoDB allows strings for numbers; title as string is valid; author.name as string is valid.
  3. Final Answer:

    Missing closing brace for the document -> Option A
  4. Quick Check:

    Every { must have matching } [OK]
Hint: Count opening and closing braces carefully [OK]
Common Mistakes:
  • Ignoring missing braces
  • Thinking string numbers are invalid
  • Assuming arrays are required for nested objects
5. You want to store a product with multiple colors and a supplier's contact info inside one MongoDB document. Which structure correctly models this?
hard
A. { "product": "Shirt", "colors": "red, blue", "supplier": "ABC Co, 123-456" }
B. { "product": "Shirt", "colors": ("red", "blue"), "supplier": { "name": "ABC Co", "phone": 123456 } }
C. { "product": "Shirt", "colors": { "red": true, "blue": true }, "supplier": ["ABC Co", "123-456"] }
D. { "product": "Shirt", "colors": ["red", "blue"], "supplier": { "name": "ABC Co", "phone": "123-456" } }

Solution

  1. Step 1: Understand how to store multiple values and nested info

    Multiple colors should be stored as an array, and supplier info as a nested document with key-value pairs.
  2. Step 2: Evaluate each option's structure

    { "product": "Shirt", "colors": ["red", "blue"], "supplier": { "name": "ABC Co", "phone": "123-456" } } correctly uses an array for colors and a nested document for supplier. { "product": "Shirt", "colors": "red, blue", "supplier": "ABC Co, 123-456" } uses strings instead of structured data. { "product": "Shirt", "colors": { "red": true, "blue": true }, "supplier": ["ABC Co", "123-456"] } uses an object for colors incorrectly and an array for supplier. { "product": "Shirt", "colors": ("red", "blue"), "supplier": { "name": "ABC Co", "phone": 123456 } } uses parentheses which are invalid in JSON.
  3. Final Answer:

    { "product": "Shirt", "colors": ["red", "blue"], "supplier": { "name": "ABC Co", "phone": "123-456" } } -> Option D
  4. Quick Check:

    Arrays for lists, objects for nested info [OK]
Hint: Use arrays for lists, objects for nested details [OK]
Common Mistakes:
  • Using strings instead of arrays for multiple values
  • Using invalid parentheses instead of braces
  • Confusing arrays and objects for nested data