0
0
MongoDBquery~20 mins

Dot notation for embedded documents in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dot Notation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find the city of the first user
Given the collection users where each document has an embedded address field with a city subfield, what is the output of this query?

db.users.findOne({}, {"address.city": 1, _id: 0})
MongoDB
db.users.insertMany([
  {name: "Alice", address: {city: "New York", zip: "10001"}},
  {name: "Bob", address: {city: "Los Angeles", zip: "90001"}}
])
A{"address": "New York"}
B{"address": {"city": "New York"}}
C{"city": "New York"}
D{"address.city": "New York"}
Attempts:
2 left
💡 Hint
Remember that dot notation in projection returns the embedded document with only the specified field.
query_result
intermediate
2:00remaining
Retrieve nested field using dot notation
Consider a collection orders with documents like:
{
  _id: 1,
  customer: {name: "John", contact: {email: "john@example.com", phone: "123-456"}},
  total: 100
}

What does this query return?

db.orders.findOne({}, {"customer.contact.email": 1, _id: 0})
MongoDB
db.orders.insertOne({
  _id: 1,
  customer: {name: "John", contact: {email: "john@example.com", phone: "123-456"}},
  total: 100
})
A{"contact": {"email": "john@example.com"}}
B{"customer.contact.email": "john@example.com"}
C{"email": "john@example.com"}
D{"customer": {"contact": {"email": "john@example.com"}}}
Attempts:
2 left
💡 Hint
Projection with dot notation returns the full path of embedded documents with only the specified leaf field.
📝 Syntax
advanced
2:00remaining
Identify the invalid dot notation query
Which of the following MongoDB queries using dot notation will cause a syntax error or fail to run?
Adb.products.find({}, {"details.price": 1})
Bdb.products.find({}, {"details.price": 1, _id: 0})
Cdb.products.find({}, {details.price: 1, _id: 0})
D)}0 :di_ ,1 :"ecirp.sliated"{ ,}{(dnif.stcudorp.bd
Attempts:
2 left
💡 Hint
Check how keys with dots must be quoted in JavaScript objects.
optimization
advanced
2:00remaining
Optimize query to return only nested field values
You want to retrieve only the score field inside the embedded stats document from all documents in players. Which query is the most efficient to return only the score values without extra nesting?
Adb.players.aggregate([{ $project: { score: "$stats.score", _id: 0 } }])
Bdb.players.find({}, {score: "$stats.score", _id: 0})
Cdb.players.find({}, {score: 1, _id: 0})
Ddb.players.find({}, {"stats.score": 1, _id: 0})
Attempts:
2 left
💡 Hint
Projection with dot notation returns nested documents; aggregation can reshape fields.
🧠 Conceptual
expert
3:00remaining
Understanding dot notation behavior with arrays
Given a collection books with documents like:
{
  title: "Book A",
  authors: [
    {name: "Alice", age: 30},
    {name: "Bob", age: 40}
  ]
}

What does the query db.books.findOne({}, {"authors.name": 1, _id: 0}) return?
A{"authors": [{"name": "Alice"}, {"name": "Bob"}]}
B{"authors.name": ["Alice", "Bob"]}
C{"name": ["Alice", "Bob"]}
D{"authors": "Alice, Bob"}
Attempts:
2 left
💡 Hint
Projection with dot notation on arrays returns the array with only the specified fields in each element.