简体   繁体   English

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

[英]Show only records with count greater than certain number using aggregation

I am trying to only show the records which have a sum greater than or equal to four.我试图只显示总和大于或等于四的记录。 This isn't working.这是行不通的。

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,
        
      },
      
    },
    
  },
  
])

There is a mismatch.存在不匹配。 Your key is totalBookings but not sum您的关键是totalBookings但不是sum

EDIT:编辑:

Refer this参考这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM