MongoDB - Aggregations using $group and functions

Let us understand how we can perform aggregations using $group and functions as part of aggregation framework.

Get order_id and order_revenue from order_details

db.orderDetails.aggregate([
  {
    $unwind: "$order_item_details"
  },
  {
    $project: 
      { 
        "order_id": 1,
        "order_item_subtotal": "$order_item_details.order_item_subtotal",
        "_id": 0
      }
  },
  {
    $group:
      {
        "_id": "$order_id",
        "order_revenue": { $sum: "$order_item_subtotal" }
      }
  }
]).pretty()