Grokking MongoDB
0% completed
MongoDB Aggregation $first
Which order came first for each product? Answering this question takes two steps: put the orders in date order, then take the first one from each product's group. The $first operator performs the second step, and this lesson shows why the first step cannot be left out.
The orders are the same five from the fruit shop, this time with their dates.
db.orders.drop() db.orders.insertMany([ { order_id: 1, product: "apple", quantity: 10, date: new Date("2023-01-01") }, { order_id: 2, product: "banana", quantity: 5, date: new Date("2023-01-02") },
.....
.....
.....
Like the course? Get enrolled and start learning!
Reading Progress
0%