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.

Persisting aggregation results with $out

Persisting aggregation results with $out

- [Instructor] In the last video, we learned how to add fields and remove fields using the $addFields and $unset aggregation stages. We use these to create a pipeline that would help us offer a promotion in our store and hopefully help us sell some of the products that we had large quantities of. Our aggregation pipeline to generate this sale look like this. In the $match stage, we are getting all of the product that we have a quantity greater than 500 of. And then, based on the product price, we apply a discount using the $addFields stage. From this discount, we also calculate what the new sales price should be again using another $addField stage. And then finally, we remove the quantity property from the results set in the $unset stage. Now, it would be fairly inefficient to have to run this aggregation every single time we wanted to get a list of products that should be on sale. Instead, we can use the $out operator…

Contents