From the course: MongoDB Essential Training

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

$group

$group

- [Instructor] Grouping data is a common pattern we need when accessing or manipulating data. Let's look at this in the terminal. In MongoDB, you can group data by using the $group stage in an aggregation pipeline. For this example, we are going to be using our sample data. In particular we are going to be using the inventory collection so let's just remind ourselves of what that looks like. Okay, let's test out the $group operator. First I'm going to specify the collection on which we're going to perform the aggregation and then I'm going to call the aggregate method. The aggregate method always takes an array of stages as input. Each stage is inside a document, so that's where we start. And then we're just going to specify the $group operator, which also takes a document as its value. Inside the document, you have to specify an ID. The ID is the thing by which the data is going to be grouped. In our case, I'm…

Contents