Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to get the first document's name in each group.
MongoDB
{ $group: { _id: "$category", firstName: { [1]: "$name" } } } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using $last instead of $first will return the last document's value.
Using $sum or $avg will cause errors or unexpected results.
✗ Incorrect
The $first accumulator returns the first value for the specified field in each group.
2fill in blank
mediumComplete the code to get the last document's score in each group.
MongoDB
{ $group: { _id: "$team", lastScore: { [1]: "$score" } } } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using $first will return the first document's value, not the last.
Using $max or $min will return the maximum or minimum values, not the last.
✗ Incorrect
The $last accumulator returns the last value for the specified field in each group.
3fill in blank
hardFix the error in the aggregation pipeline to correctly get the first and last names.
MongoDB
[ { $group: { _id: "$department", firstEmp: { [1]: "$employee" }, lastEmp: { $last: "$employee" } } } ] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using $sum or $avg causes errors because they are not valid for string fields.
Using $max returns the maximum value, not the first.
✗ Incorrect
The $first accumulator is needed to get the first employee's name; $last is already correct.
4fill in blank
hardFill both blanks to get the first and last order dates for each customer.
MongoDB
{ $group: { _id: "$customerId", firstOrder: { [1]: "$orderDate" }, lastOrder: { [2]: "$orderDate" } } } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping $first and $last will give incorrect results.
Using $max or $min will return the highest or lowest dates, not the first or last.
✗ Incorrect
Use $first for the first order date and $last for the last order date in each group.
5fill in blank
hardFill all three blanks to get the first product name, last product name, and maximum price in each category.
MongoDB
{ $group: { _id: "$category", firstProduct: { [1]: "$productName" }, lastProduct: { [2]: "$productName" }, maxPrice: { [3]: "$price" } } } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using $min instead of $max for price will give the lowest price, not the highest.
Mixing up $first and $last will swap the product names incorrectly.
✗ Incorrect
Use $first for the first product, $last for the last product, and $max for the maximum price.