0
0
MongoDBquery~20 mins

Insert with nested documents in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.