0
0
MongoDBquery~20 mins

$first and $last accumulators in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB $first and $last Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this MongoDB aggregation using $first?
Consider a collection named sales with documents:
{ "_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" }
  } }
])
A[{ "_id": "apple", "firstQty": 5 }]
B[{ "_id": "apple", "firstQty": 7 }]
C[{ "_id": "apple", "firstQty": 10 }]
D[{ "_id": "apple", "firstQty": null }]
Attempts:
2 left
💡 Hint
Remember $first returns the value from the first document in the group after sorting.
query_result
intermediate
2: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" }
  } }
])
A[{ "_id": "apple", "lastQty": 5 }]
B[{ "_id": "apple", "lastQty": 7 }]
C[{ "_id": "apple", "lastQty": 10 }]
D[{ "_id": "apple", "lastQty": null }]
Attempts:
2 left
💡 Hint
Think about the last document after sorting by date ascending.
📝 Syntax
advanced
2: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?
A{ $group: { _id: "$category", earliestDate: { $first: "$dates" } } }
B{ $group: { _id: "$category", earliestDate: { $first: { $date: "$date" } } } }
C{ $group: { _id: "$category", earliestDate: { $first: "$category.date" } } }
D{ $group: { _id: "$category", earliestDate: { $first: "$date" } } }
Attempts:
2 left
💡 Hint
Use the field name directly inside $first after sorting.
🔧 Debug
advanced
2:00remaining
Why does this aggregation return null for $last?
Given this aggregation:
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" } } }
])
ABecause the documents are not sorted before grouping, so $last returns the last document in an unordered group, which can be null.
BBecause the field name "orderDate" is misspelled or missing in documents.
CBecause $last only works on arrays, not on fields.
DBecause $last requires a $sort stage before $group to work correctly.
Attempts:
2 left
💡 Hint
Check if the field exists in the documents.
🧠 Conceptual
expert
2: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?
A$first and $last return values from documents in the natural order of the input collection, which is undefined and may vary.
B$first and $last always return the earliest and latest values by date automatically.
C$first returns the minimum value and $last returns the maximum value of the grouped field.
D$first and $last cause an error if $sort is not used before $group.
Attempts:
2 left
💡 Hint
Think about how MongoDB processes documents without explicit sorting.