Bird
Raised Fist0
MongoDBquery~20 mins

Insert with nested documents 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
🎖️
Nested Document Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Insert a document with nested address
Given the following insert command, what will be the content of the inserted document in the collection?
MongoDB
db.users.insertOne({name: "Alice", age: 30, address: {street: "123 Maple St", city: "Springfield", zip: "01101"}})
A{"name": "Alice", "age": 30, "address": {"street": "123 Maple St", "city": "Springfield"}}
B{"name": "Alice", "age": 30, "address": "123 Maple St, Springfield, 01101"}
C{"name": "Alice", "age": 30, "street": "123 Maple St", "city": "Springfield", "zip": "01101"}
D{"name": "Alice", "age": 30, "address": {"street": "123 Maple St", "city": "Springfield", "zip": "01101"}}
Attempts:
2 left
💡 Hint
Nested documents are stored as embedded objects inside the main document.
query_result
intermediate
2:00remaining
Insert with multiple nested levels
What will be the structure of the document after this insert?
MongoDB
db.orders.insertOne({orderId: 101, customer: {name: "Bob", contact: {email: "bob@example.com", phone: "123-4567"}}, items: [{product: "Pen", qty: 10}, {product: "Notebook", qty: 5}]})
A{"orderId": 101, "customer": {"name": "Bob", "contact": {"email": "bob@example.com", "phone": "123-4567"}}, "items": [{"product": "Pen", "qty": 10}, {"product": "Notebook", "qty": 5}]}
B{"orderId": 101, "customer": {"name": "Bob", "email": "bob@example.com", "phone": "123-4567"}, "items": [{"product": "Pen", "qty": 10}, {"product": "Notebook", "qty": 5}]}
C{"orderId": 101, "customer": "Bob", "contact": {"email": "bob@example.com", "phone": "123-4567"}, "items": [{"product": "Pen", "qty": 10}, {"product": "Notebook", "qty": 5}]}
D{"orderId": 101, "customer": {"name": "Bob", "contact": {"email": "bob@example.com"}}, "items": [{"product": "Pen", "qty": 10}, {"product": "Notebook", "qty": 5}]}
Attempts:
2 left
💡 Hint
Nested objects can have multiple levels and arrays can contain objects.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in nested document insert
Which option contains a syntax error that will prevent the document from being inserted?
MongoDB
db.products.insertOne({name: "Chair", specs: {color: "red", weight: 5, dimensions: {width: 40, height: 90}}})
Adb.products.insertOne({name: "Chair", specs: {color: "red", weight: 5, dimensions: {width: 40, height: 90}}}})
Bdb.products.insertOne({name: "Chair", specs: {color: "red", weight: 5, dimensions: {width: 40, height: 90}}})
Cdb.products.insertOne({name: "Chair", specs: {color: "red", weight: 5, dimensions: {width: 40, height: 90}}
Ddb.products.insertOne({name: "Chair", specs: {color: "red", weight: 5, dimensions: {width: 40, height: 90}});
Attempts:
2 left
💡 Hint
Check for matching braces and parentheses.
🔧 Debug
advanced
2:00remaining
Why does this nested insert fail?
This insert command fails. What is the cause?
MongoDB
db.employees.insertOne({name: "Eve", details: {age: 28, skills: ["JavaScript", "MongoDB"], projects: {current: "Website", completed: 3}})
AMissing closing brace for the main document.
BArray syntax is incorrect inside 'skills'.
CField names must be quoted with double quotes.
DMongoDB does not support nested documents.
Attempts:
2 left
💡 Hint
Count the opening and closing braces carefully.
optimization
expert
3:00remaining
Optimizing nested document insert for large arrays
You want to insert a document with a large array of nested documents efficiently. Which approach is best?
AInsert the main document first, then update it multiple times to add array elements one by one.
BInsert the entire document with the large nested array in one insertOne command.
CSplit the large array into smaller chunks and insert multiple documents instead of one.
DUse bulkWrite with multiple insertOne operations for each nested document inside the array.
Attempts:
2 left
💡 Hint
MongoDB handles nested arrays well if inserted in one document.

Practice

(1/5)
1. What is the main purpose of using nested documents in MongoDB inserts?
easy
A. To create multiple collections at once
B. To group related data inside one record
C. To speed up query execution automatically
D. To enforce strict schema validation

Solution

  1. Step 1: Understand nested documents concept

    Nested documents allow storing related data together inside a single MongoDB document using curly braces { }.
  2. Step 2: Identify the purpose of grouping data

    This grouping helps keep related information organized and easy to access in one record.
  3. Final Answer:

    To group related data inside one record -> Option B
  4. Quick Check:

    Nested documents = Group related data [OK]
Hint: Nested documents group related info inside one record [OK]
Common Mistakes:
  • Thinking nested documents create multiple collections
  • Assuming nested documents speed up queries automatically
  • Confusing nested documents with schema enforcement
2. Which of the following is the correct syntax to insert a nested document using insertOne in MongoDB?
easy
A. db.collection.insertOne({name: "Alice", address: {city: "NY", zip: 10001}})
B. db.collection.insertOne([name: "Alice", address: {city: "NY", zip: 10001}])
C. db.collection.insertOne({name: "Alice", address: [city: "NY", zip: 10001]})
D. db.collection.insertOne({name: "Alice", address: "city: NY, zip: 10001"})

Solution

  1. Step 1: Check the correct use of curly braces for nested documents

    Nested documents must be inside curly braces { }, not square brackets [ ].
  2. Step 2: Verify the overall insertOne syntax

    The insertOne method takes a single document object with key-value pairs, including nested documents correctly enclosed in braces.
  3. Final Answer:

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

    Curly braces for nested docs + insertOne syntax = db.collection.insertOne({name: "Alice", address: {city: "NY", zip: 10001}}) [OK]
Hint: Use curly braces { } for nested docs inside insertOne [OK]
Common Mistakes:
  • Using square brackets instead of curly braces for nested docs
  • Passing an array instead of an object to insertOne
  • Putting nested data as a string instead of an object
3. Given the following insert command, what will be the stored document in the collection?
db.users.insertOne({name: "Bob", contact: {email: "bob@example.com", phone: "123-456"}})
medium
A. { "name": "Bob", "contact": { "email": "bob@example.com", "phone": "123-456" } }
B. { "name": "Bob", "contact.email": "bob@example.com", "contact.phone": "123-456" }
C. { "name": "Bob", "contact": [ "email", "phone" ] }
D. { "name": "Bob", "email": "bob@example.com", "phone": "123-456" }

Solution

  1. Step 1: Understand how nested documents are stored

    The nested document under 'contact' is stored as a sub-document with keys 'email' and 'phone'.
  2. Step 2: Compare options with expected nested structure

    Only { "name": "Bob", "contact": { "email": "bob@example.com", "phone": "123-456" } } correctly shows 'contact' as an object containing 'email' and 'phone' keys.
  3. Final Answer:

    { "name": "Bob", "contact": { "email": "bob@example.com", "phone": "123-456" } } -> Option A
  4. Quick Check:

    Nested document stored as object = { "name": "Bob", "contact": { "email": "bob@example.com", "phone": "123-456" } } [OK]
Hint: Nested documents appear as objects inside main document [OK]
Common Mistakes:
  • Flattening nested keys incorrectly
  • Using arrays instead of objects for nested data
  • Placing nested fields at top level
4. Identify the error in this MongoDB insert command:
db.products.insertOne({name: "Pen", details: [color: "blue", price: 1.5]})
medium
A. Missing comma between fields in main document
B. Missing quotes around field names
C. Using square brackets instead of curly braces for nested document
D. insertOne cannot insert nested documents

Solution

  1. Step 1: Check syntax for nested documents

    Nested documents must use curly braces { }, but here square brackets [ ] are used incorrectly.
  2. Step 2: Confirm other syntax elements are correct

    Field names are quoted correctly or allowed without quotes, and commas are present; insertOne supports nested docs.
  3. Final Answer:

    Using square brackets instead of curly braces for nested document -> Option C
  4. Quick Check:

    Nested docs need { }, not [ ] [OK]
Hint: Nested docs require curly braces { }, not square brackets [ ] [OK]
Common Mistakes:
  • Confusing arrays with nested documents
  • Thinking insertOne can't insert nested docs
  • Forgetting commas between fields
5. You want to insert multiple user profiles with nested address documents using insertMany. Which command correctly inserts two users with nested addresses?
hard
A. db.users.insertMany([{name: "Anna", address: [city: "LA", zip: 90001]}, {name: "Mark", address: [city: "SF", zip: 94105]}])
B. db.users.insertMany({name: "Anna", address: {city: "LA", zip: 90001}}, {name: "Mark", address: {city: "SF", zip: 94105}})
C. db.users.insertMany([{name: "Anna", address: "city: LA, zip: 90001"}, {name: "Mark", address: "city: SF, zip: 94105"}])
D. db.users.insertMany([{name: "Anna", address: {city: "LA", zip: 90001}}, {name: "Mark", address: {city: "SF", zip: 94105}}])

Solution

  1. Step 1: Verify insertMany syntax with array of documents

    insertMany requires an array of document objects, each with nested documents using curly braces.
  2. Step 2: Check nested document syntax inside each user

    Nested 'address' fields must be objects with curly braces, not arrays or strings.
  3. Final Answer:

    db.users.insertMany([{name: "Anna", address: {city: "LA", zip: 90001}}, {name: "Mark", address: {city: "SF", zip: 94105}}]) -> Option D
  4. Quick Check:

    insertMany + array + nested docs with { } = db.users.insertMany([{name: "Anna", address: {city: "LA", zip: 90001}}, {name: "Mark", address: {city: "SF", zip: 94105}}]) [OK]
Hint: Use array of objects with nested docs in insertMany [OK]
Common Mistakes:
  • Passing multiple documents as separate arguments instead of array
  • Using arrays instead of objects for nested documents
  • Putting nested data as strings instead of objects