Bird
Raised Fist0
MongoDBquery~20 mins

Rows vs documents thinking in MongoDB - Practice Questions

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 Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding document structure vs row structure
In MongoDB, which statement best describes how data is stored compared to a traditional SQL row?
AData is stored as flexible documents that can contain nested arrays and objects, unlike fixed columns in SQL rows.
BData is stored strictly in tables with fixed columns and rows, similar to SQL databases.
CEach document in MongoDB must have the same fields and data types, just like SQL rows.
DMongoDB stores data only as key-value pairs without any nested structure.
Attempts:
2 left
💡 Hint
Think about how JSON-like documents allow more flexible data than rows.
query_result
intermediate
2:00remaining
Query result shape in MongoDB vs SQL
Given a MongoDB collection 'users' with documents containing nested 'address' objects, what will this query return? `db.users.find({"address.city": "Seattle"})`
MongoDB
db.users.find({"address.city": "Seattle"})
AOnly the 'address' subdocuments where city is 'Seattle', excluding other user data.
BAll user documents where the nested 'address' object has a 'city' field equal to 'Seattle'.
CAn error because nested fields cannot be queried in MongoDB.
DAll users regardless of city because nested queries are ignored.
Attempts:
2 left
💡 Hint
MongoDB allows querying nested fields using dot notation.
📝 Syntax
advanced
2:00remaining
Correct MongoDB query to update nested document field
Which of the following MongoDB update queries correctly sets the 'address.zip' field to '98101' for user with _id 123?
Adb.users.updateOne({_id: 123}, {$update: {"address.zip": "98101"}})
Bdb.users.updateOne({_id: 123}, {$set: {address: {zip: "98101"}}})
Cdb.users.updateOne({_id: 123}, {$set: {"address.zip": "98101"}})
Ddb.users.update({_id: 123}, {$set: {zip: "98101"}})
Attempts:
2 left
💡 Hint
Use dot notation inside $set to update nested fields.
optimization
advanced
2:00remaining
Optimizing queries on nested document fields
You have a large MongoDB collection with documents containing nested 'profile.age' fields. Which indexing strategy will best optimize queries filtering by 'profile.age'?
ACreate a single-field index on 'profile.age'.
BCreate a compound index on 'profile' and 'age' separately.
CCreate a text index on the entire document.
DNo index is needed because nested fields cannot be indexed.
Attempts:
2 left
💡 Hint
Indexes on nested fields use dot notation.
🔧 Debug
expert
3:00remaining
Diagnosing unexpected query results with nested documents
A developer runs this query on a MongoDB collection: `db.orders.find({"customer.address.city": "Boston"})` But it returns no results, even though some documents have 'customer' with nested 'address.city' equal to 'Boston'. What is the most likely cause?
AMongoDB does not support querying nested fields with dot notation.
BThe field name 'city' is case-sensitive and should be 'City' instead.
CThe query syntax is invalid because quotes are missing around the field name.
DThe 'customer' field is an array of objects, so dot notation does not match nested fields inside arrays without $elemMatch.
Attempts:
2 left
💡 Hint
Consider how nested arrays affect dot notation queries.

Practice

(1/5)
1. Which statement best describes the difference between rows in SQL and documents in MongoDB?
easy
A. Rows are flexible and can change structure easily; documents are rigid.
B. Rows can store nested data; documents only store flat data.
C. Rows have fixed columns; documents can have varied fields and nested data.
D. Rows and documents are exactly the same in structure and use.

Solution

  1. Step 1: Understand row structure in SQL

    Rows have fixed columns defined by the table schema, so each row has the same fields.
  2. Step 2: Understand document structure in MongoDB

    Documents can have different fields and nested objects, allowing flexible and varied data.
  3. Final Answer:

    Rows have fixed columns; documents can have varied fields and nested data. -> Option C
  4. Quick Check:

    Rows = fixed, Documents = flexible [OK]
Hint: Rows = fixed columns, documents = flexible fields [OK]
Common Mistakes:
  • Thinking documents must have same fields like rows
  • Assuming rows can store nested data easily
  • Confusing flexibility of documents with rows
2. Which of the following is the correct way to insert a document with nested fields in MongoDB?
easy
A. db.collection.insertOne({name: 'Alice', address: {city: 'NY', zip: 10001}})
B. INSERT INTO collection VALUES ('Alice', {city: 'NY', zip: 10001})
C. db.collection.insertOne(name: 'Alice', address: 'NY')
D. INSERT document {name: 'Alice', address: {city: 'NY', zip: 10001}}

Solution

  1. Step 1: Identify MongoDB insert syntax

    MongoDB uses db.collection.insertOne() with a JSON-like document as argument.
  2. Step 2: Check nested field format

    Nested fields are represented as nested objects inside the document, like address: {city: 'NY', zip: 10001}.
  3. Final Answer:

    db.collection.insertOne({name: 'Alice', address: {city: 'NY', zip: 10001}}) -> Option A
  4. Quick Check:

    Correct MongoDB insert with nested document [OK]
Hint: Use insertOne() with nested JSON object for documents [OK]
Common Mistakes:
  • Using SQL INSERT syntax in MongoDB
  • Passing fields outside an object
  • Not using curly braces for nested fields
3. Given the collection users with documents like {name: 'Bob', scores: [10, 20, 30]}, what will db.users.find({scores: 20}) return?
medium
A. All documents where scores array contains 20
B. Documents where scores equals exactly 20
C. Documents where scores is missing
D. Syntax error because scores is an array

Solution

  1. Step 1: Understand MongoDB query on array fields

    Querying {scores: 20} matches documents where the array 'scores' contains the value 20.
  2. Step 2: Apply to example document

    Document with scores: [10, 20, 30] contains 20, so it matches and will be returned.
  3. Final Answer:

    All documents where scores array contains 20 -> Option A
  4. Quick Check:

    Query matches array elements [OK]
Hint: Querying array field matches if any element equals value [OK]
Common Mistakes:
  • Thinking query matches whole array exactly
  • Expecting syntax error for array query
  • Confusing missing field with array content
4. You run this MongoDB query: db.orders.find({items: {product: 'Book'}}) but get no results. What is the likely problem?
medium
A. The field name 'product' is misspelled.
B. The query expects items to be an object, but items is an array of objects.
C. MongoDB does not support querying nested fields.
D. The collection name 'orders' is incorrect.

Solution

  1. Step 1: Analyze query structure

    The query matches documents where 'items' exactly equals {product: 'Book'}.
  2. Step 2: Understand data structure

    If 'items' is an array of objects, the query must use dot notation or $elemMatch to match inside array elements.
  3. Final Answer:

    The query expects items to be an object, but items is an array of objects. -> Option B
  4. Quick Check:

    Querying array needs $elemMatch or dot notation [OK]
Hint: Use $elemMatch or dot notation to query inside arrays [OK]
Common Mistakes:
  • Querying array field as if it was a single object
  • Assuming MongoDB can't query nested fields
  • Ignoring data structure of the field
5. You want to store customer orders where each order can have multiple items with different quantities and prices. Which data model fits best in MongoDB?
hard
A. One document per item, with order info duplicated in each document.
B. Separate collections for orders and items, no embedding.
C. One flat table with one row per item, no nesting.
D. One document per order, with an array of item objects inside each document.

Solution

  1. Step 1: Understand MongoDB document flexibility

    MongoDB documents can embed arrays of objects, ideal for storing multiple items inside one order document.
  2. Step 2: Compare options for data modeling

    Embedding items inside order documents keeps related data together and fits MongoDB's document model better than duplication or flat tables.
  3. Final Answer:

    One document per order, with an array of item objects inside each document. -> Option D
  4. Quick Check:

    Embed related data in arrays for flexible documents [OK]
Hint: Embed related items as arrays inside order documents [OK]
Common Mistakes:
  • Duplicating order info in many documents
  • Using flat tables like SQL in MongoDB
  • Splitting related data unnecessarily