Docs Menu

Docs HomeDevelop ApplicationsMongoDB DriversNode.js Driver

Group and Total

On this page

  • Introduction
  • Aggregation Task Summary
  • Before You Get Started
  • Tutorial
  • Add a match stage for orders in 2020
  • Add a sort stage to sort by order date
  • Add a group stage to group by email address
  • Add a sort stage to sort by first order date
  • Add a set stage to display the email address
  • Add an unset stage to remove unneeded fields
  • Run the aggregation pipeline
  • Interpret results

In this tutorial, you can learn how to use the Node.js driver to construct an aggregation pipeline, perform the aggregation on a collection, and print the results by completing and running a sample app. This aggregation performs the following operations:

  • Matches a subset of documents by a field value

  • Groups documents by common field values

  • Adds computed fields to each result document

This tutorial demonstrates how to group and analyze customer order data. The results show the list of customers who purchased items in 2020 and includes each customer's order history for 2020.

This example uses one collection, orders, which contains documents describing individual product orders. Since each order can correspond to only one customer, the order documents are grouped by the customer_id field, which contains customer email addresses.

Before you start this tutorial, complete the Aggregation Template App instructions to set up a working Node.js application.

After you set up the app, access the orders collection by adding the following code to the application:

const ordersColl = await aggDB.collection("orders");

Delete any existing data and insert sample data into the orders collection as shown in the following code:

await ordersColl.deleteMany({});
const orderData = [
{
customer_id: "elise_smith@myemail.com",
orderdate: new Date("2020-05-30T08:35:52Z"),
value: 231,
},
{
customer_id: "elise_smith@myemail.com",
orderdate: new Date("2020-01-13T09:32:07Z"),
value: 99,
},
{
customer_id: "oranieri@warmmail.com",
orderdate: new Date("2020-01-01T08:25:37Z"),
value: 63,
},
{
customer_id: "tj@wheresmyemail.com",
orderdate: new Date("2019-05-28T19:13:32Z"),
value: 2,
},
{
customer_id: "tj@wheresmyemail.com",
orderdate: new Date("2020-11-23T22:56:53Z"),
value: 187,
},
{
customer_id: "tj@wheresmyemail.com",
orderdate: new Date("2020-08-18T23:04:48Z"),
value: 4,
},
{
customer_id: "elise_smith@myemail.com",
orderdate: new Date("2020-12-26T08:55:46Z"),
value: 4,
},
{
customer_id: "tj@wheresmyemail.com",
orderdate: new Date("2021-02-29T07:49:32Z"),
value: 1024,
},
{
customer_id: "elise_smith@myemail.com",
orderdate: new Date("2020-10-03T13:49:44Z"),
value: 102,
},
];
await ordersColl.insertMany(orderData);
1

First, add a $match stage that matches orders placed in 2020:

pipeline.push({
$match: {
orderdate: {
$gte: new Date("2020-01-01T00:00:00Z"),
$lt: new Date("2021-01-01T00:00:00Z"),
},
},
});
2

Next, add a $sort stage to set an ascending sort on the orderdate field to surface the earliest 2020 purchase for each customer in the next stage:

pipeline.push({
$sort: {
orderdate: 1,
},
});
3

Add a $group stage to group orders by the value of the customer_id field. In this stage, add aggregation operations that create the following fields in the result documents:

  • first_purchase_date: the date of the customer's first purchase

  • total_value: the total value of all the customer's purchases

  • total_orders: the total number of the customer's purchases

  • orders: the list of all the customer's purchases, including the date and value of each purchase

pipeline.push({
$group: {
_id: "$customer_id",
first_purchase_date: { $first: "$orderdate" },
total_value: { $sum: "$value" },
total_orders: { $sum: 1 },
orders: { $push:
{
orderdate: "$orderdate",
value: "$value"
}
},
},
});
4

Next, add another $sort stage to set an ascending sort on the first_purchase_date field:

pipeline.push({
$sort: {
first_purchase_date: 1,
},
});
5

Add a $set stage to recreate the customer_id field from the values in the _id field that were set during the $group stage:

pipeline.push({
$set: {
customer_id: "$_id",
},
});
6

Finally, add an $unset stage. The $unset stage removes the _id field from the result documents:

pipeline.push({ $unset: ["_id"] });
7

Add the following code to the end of your application to perform the aggregation on the orders collection:

const aggregationResult = await ordersColl.aggregate(pipeline);

Finally, run the following command in your shell to start your application:

node agg_tutorial.js
8

The aggregation returns the following summary of customers' orders from 2020:

{
first_purchase_date: 2020-01-01T08:25:37.000Z,
total_value: 63,
total_orders: 1,
orders: [ { orderdate: 2020-01-01T08:25:37.000Z, value: 63 } ],
customer_id: 'oranieri@warmmail.com'
}
{
first_purchase_date: 2020-01-13T09:32:07.000Z,
total_value: 436,
total_orders: 4,
orders: [
{ orderdate: 2020-01-13T09:32:07.000Z, value: 99 },
{ orderdate: 2020-05-30T08:35:52.000Z, value: 231 },
{ orderdate: 2020-10-03T13:49:44.000Z, value: 102 },
{ orderdate: 2020-12-26T08:55:46.000Z, value: 4 }
],
customer_id: 'elise_smith@myemail.com'
}
{
first_purchase_date: 2020-08-18T23:04:48.000Z,
total_value: 191,
total_orders: 2,
orders: [
{ orderdate: 2020-08-18T23:04:48.000Z, value: 4 },
{ orderdate: 2020-11-23T22:56:53.000Z, value: 187 }
],
customer_id: 'tj@wheresmyemail.com'
}

The result documents contain details from all the orders from a given customer, grouped by the customer's email address.

To view the complete code for this tutorial, see the Completed Group and Total App on GitHub.

← Filtered Subset