MongoDB: Calculate total sales amount per product using aggregation.

Code Playground
JavaScript (V8)
Loading...
Console Output
// Output will appear here...
// For React: ReactDOM.createRoot(rootElement).render(<App />);
Recommended Answer
js
db.sales.aggregate([
  {
    $group: {
      _id: "$product_id",
      totalAmount: { $sum: "$amount" }
    }
  }
]);