使用聚合仅显示计数大于特定数量的记录

Show only records with count greater than certain number using aggregation

我试图只显示总和大于或等于四的记录。这不起作用。

const bookings = await Booking.aggregate([
  {
    $group: {
      _id: { $dateToString: { format: "%Y-%m-%d", date: "$bookingDate" } },
      totalBookings: {
        $sum: 1,
      },
    },
    $match: {
      sum: { $gte: 4 },
    },
  },
]);
db.collection.aggregate([
  {
    $group: {
      _id: {
        $dateToString: {
          format: "%Y-%m-%d",
          date: "$bookingDate",
          
        },
        
      },
      totalBookings: {
        $sum: 1,
        
      },
      
    }
  },
  {
    $match: {
      totalBookings: {
        $gte: 4,
        
      },
      
    },
    
  },
  
])

不匹配。您的密钥是 totalBookings 但不是 sum

编辑:

Refer this