From the course: MongoDB Aggregation Pipeline: Advanced Data Analysis and Manipulation
Unlock the full course today
Join today to access over 24,500 courses taught by industry experts.
Custom aggregation accumulator - MongoDB Tutorial
From the course: MongoDB Aggregation Pipeline: Advanced Data Analysis and Manipulation
Custom aggregation accumulator
- [Instructor] In the last video, we learned how we can write custom JavaScript functions to extend the MongoDB query language using the $function aggregator. In this video, we'll learn how to implement a custom accumulator using the $accumulator operator. For our use case, we want to create an accumulator that will tell us how many orders each customer has placed and what their total spend was. Additionally, based on the information generated, if a customer has met a certain threshold, we want to mark them as VIP. The $accumulator operator takes up to seven properties, and they are: the init function, the accumulate function, the merge function, and an optional finalize function. Then we can pass in parameters to the init and accumulate functions using the initArgs and accumulateArgs properties. And finally, we are required to set a lang for language, which in our case again will just be JavaScript. And we'll go into…