Efficient way to get data for each date range intervals using single query | MongoDB
If you have only 4 intervals then this could be an approach:
OrdersModel.aggregate([
   {
      $facet: {
         interval1: [
            { $match: { orderDate: { $gte: new Date(sdt_1), $lte: new Date(edt_1) } } },
            {
               $group:
                  {
                     _id: &quot;$product_name&quot;, //{ name: &quot;$name&quot;},
                     totalQuantity: { $sum: &quot;$quantity&quot; },
                  }
            },
            { $project: { _id: 1, qty: &quot;$totalQuantity&quot;, } }
         ],
         interval2: [
            { $match: { orderDate: { $gte: new Date(sdt_2), $lte: new Date(edt_2) } } },
            {
               $group:
                  {
                     _id: &quot;$product_name&quot;, //{ name: &quot;$name&quot;},
                     totalQuantity: { $sum: &quot;$quantity&quot; },
                  }
            },
            { $project: { _id: 1, qty: &quot;$totalQuantity&quot;, } }
         ],
      }
   }
])

A more dynamic way would be this one:
var facets = {};

facets[&quot;interval1&quot;] = [
   { $match: { orderDate: { $gte: new Date(sdt_1), $lte: new Date(edt_1) } } },
   {
      $group:
         {
            _id: &quot;$product_name&quot;, //{ name: &quot;$name&quot;},
            totalQuantity: { $sum: &quot;$quantity&quot; },
         }
   },
   { $project: { _id: 1, qty: &quot;$totalQuantity&quot;, } }
];

facets[&quot;interval2&quot;] = [
   { $match: { orderDate: { $gte: new Date(sdt_2), $lte: new Date(edt_2) } } },
   {
      $group:
         {
            _id: &quot;$product_name&quot;, //{ name: &quot;$name&quot;},
            totalQuantity: { $sum: &quot;$quantity&quot; },
         }
   },
   { $project: { _id: 1, qty: &quot;$totalQuantity&quot;, } }
];


facets[&quot;interval3&quot;] = [];
facets[&quot;interval3&quot;].push({ $match: { orderDate: { $gte: new Date(sdt_3), $lte: new Date(edt_3) } } });
facets[&quot;interval3&quot;].push(
   {
      $group:
         {
            _id: &quot;$product_name&quot;, //{ name: &quot;$name&quot;},
            totalQuantity: { $sum: &quot;$quantity&quot; },
         }
   },
   { $project: { _id: 1, qty: &quot;$totalQuantity&quot;, } }
);

OrdersModel.aggregate([
   {
      $facet: facets
   }
])