From the course: MongoDB Essential Training

Unlock the full course today

Join today to access over 24,100 courses taught by industry experts.

$bucket

$bucket

- [Instructor] Much like the dollar group stage, the dollar bucket stage allows you to put documents into groups, but instead of grouping them by one value that has to be the same for all documents, you define bucket boundaries and ranges for a document value. If the documents values fall within that range, the documents will be sorted into that bucket. Let's try this out on the terminal. Again, we're going to be using the inventory data set. As before, the aggregate method takes in an array and then we're going to put into this array a document for the dollar bucket stage. Okay. The dollar bucket stage takes a document assets value. Inside the document, you have to specify some arguments. The first one is group by, and that's the field that you want to group by. So in this case, I'm going to be grouping by dollar year 'cause I'm going to be grouping the cars by the year of manufacture. Then, we have to specify the…

Contents