0
0
MongoDBquery~20 mins

How MongoDB stores data as documents - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB Document Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What does this MongoDB document look like after insertion?
Consider the following MongoDB insert operation:

db.users.insertOne({ name: "Alice", age: 30, hobbies: ["reading", "hiking"] })

What will the stored document look like when retrieved?
MongoDB
db.users.findOne({ name: "Alice" })
A{ "name": "Alice", "age": "30", "hobbies": "reading, hiking" }
B{ "_id": ObjectId(...), "name": "Alice", "age": 30, "hobbies": ["reading", "hiking"] }
C{ "_id": ObjectId(...), "name": "Alice", "age": "30", "hobbies": ["reading", "hiking"] }
D{ "name": "Alice", "age": 30, "hobbies": ["reading", "hiking"] }
Attempts:
2 left
💡 Hint
Remember MongoDB automatically adds an _id field and preserves data types.
🧠 Conceptual
intermediate
1:30remaining
How does MongoDB store nested data?
Which of the following best describes how MongoDB stores nested data inside documents?
AAs embedded documents within the main document using key-value pairs.
BAs flat JSON strings inside a single field.
CAs separate tables linked by foreign keys.
DAs separate files on disk referenced by the document.
Attempts:
2 left
💡 Hint
Think about how JSON objects can contain other objects.
📝 Syntax
advanced
2:30remaining
Which MongoDB query correctly updates a nested field?
Given a document structure:

{ name: "Bob", address: { city: "NY", zip: 10001 } }

Which query correctly updates the city to "LA"?
Adb.collection.updateOne({ name: "Bob" }, { $set: { address: { city: "LA" } } })
Bdb.collection.updateOne({ name: "Bob" }, { city: "LA" })
Cdb.collection.updateOne({ name: "Bob" }, { $set: { "address.city": "LA" } })
Ddb.collection.updateOne({ name: "Bob" }, { $update: { "address.city": "LA" } })
Attempts:
2 left
💡 Hint
Use dot notation with $set to update nested fields.
optimization
advanced
3:00remaining
How to optimize queries on nested document fields?
You have a collection with documents containing nested fields like:

{ user: { name: "Eve", age: 25 }, status: "active" }

Which approach best improves query performance filtering by user name?
AUse a text index on the entire document.
BCreate an index on the status field only.
CFlatten the document to remove nesting.
DCreate an index on the nested field user.name.
Attempts:
2 left
💡 Hint
Indexes on fields used in queries speed up searches.
🔧 Debug
expert
3:00remaining
Why does this MongoDB query return no results?
Given documents:

{ name: "Sam", details: { age: 40, city: "Boston" } }

Which query will NOT return this document?

Options:
A) db.collection.find({ "details.age": 40 })
B) db.collection.find({ details: { age: 40, city: "Boston" } })
C) db.collection.find({ details: { city: "Boston", age: 40 } })
D) db.collection.find({ "details.city": "Boston" })
AReturns no documents because field order in embedded document matters.
BReturns the document because it matches the nested city field.
CReturns the document because exact object match requires field order.
DReturns the document because it matches the nested age field.
Attempts:
2 left
💡 Hint
Exact matches on embedded documents require the same field order.