Challenge - 5 Problems
MongoDB $first and $last Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of this MongoDB aggregation using $first?
Consider a collection named sales with documents:
What will be the result of this aggregation?
{ "_id": 1, "item": "apple", "qty": 5, "date": ISODate("2023-01-01") }{ "_id": 2, "item": "apple", "qty": 10, "date": ISODate("2023-01-02") }{ "_id": 3, "item": "apple", "qty": 7, "date": ISODate("2023-01-03") }What will be the result of this aggregation?
db.sales.aggregate([
{ $match: { item: "apple" } },
{ $sort: { date: 1 } },
{ $group: {
_id: "$item",
firstQty: { $first: "$qty" }
} }
])MongoDB
db.sales.aggregate([
{ $match: { item: "apple" } },
{ $sort: { date: 1 } },
{ $group: {
_id: "$item",
firstQty: { $first: "$qty" }
} }
])Attempts:
2 left
💡 Hint
Remember $first returns the value from the first document in the group after sorting.
✗ Incorrect
The documents are sorted by date ascending, so the first document for 'apple' has qty 5. $first returns this value.
❓ query_result
intermediate2:00remaining
What does $last return in this aggregation?
Given the same sales collection as before, what is the output of:
db.sales.aggregate([
{ $match: { item: "apple" } },
{ $sort: { date: 1 } },
{ $group: {
_id: "$item",
lastQty: { $last: "$qty" }
} }
])MongoDB
db.sales.aggregate([
{ $match: { item: "apple" } },
{ $sort: { date: 1 } },
{ $group: {
_id: "$item",
lastQty: { $last: "$qty" }
} }
])Attempts:
2 left
💡 Hint
Think about the last document after sorting by date ascending.
✗ Incorrect
After sorting by date ascending, the last document for 'apple' has qty 7, so $last returns 7.
📝 Syntax
advanced2:00remaining
Which aggregation stage correctly uses $first to get the earliest date?
You want to group documents by
category and get the earliest date for each group. Which option is correct?Attempts:
2 left
💡 Hint
Use the field name directly inside $first after sorting.
✗ Incorrect
Option D correctly uses $first on the field "$date". Other options have invalid field references or syntax.
🔧 Debug
advanced2:00remaining
Why does this aggregation return null for $last?
Given this aggregation:
It returns
db.orders.aggregate([
{ $group: { _id: "$customer", lastOrder: { $last: "$orderDate" } } }
])It returns
lastOrder: null for all customers. Why?MongoDB
db.orders.aggregate([
{ $group: { _id: "$customer", lastOrder: { $last: "$orderDate" } } }
])Attempts:
2 left
💡 Hint
Check if the field exists in the documents.
✗ Incorrect
If the field "orderDate" does not exist or is misspelled, $last returns null. Sorting is not required but affects which document is last.
🧠 Conceptual
expert2:00remaining
How do $first and $last behave without a $sort stage?
In a MongoDB aggregation pipeline, if you use $first and $last inside a $group stage without a preceding $sort, what determines the values returned?
Attempts:
2 left
💡 Hint
Think about how MongoDB processes documents without explicit sorting.
✗ Incorrect
$first and $last pick values from the first and last documents in the group as they appear in the input, which is not guaranteed without sorting.