【问题标题】:Match time only in mongodb aggregation仅在 mongodb 聚合中匹配时间
【发布时间】:2020-08-17 15:23:04
【问题描述】:

我将每天的营业时间存储为一系列班次,如下所示:

{
  Monday: [{
    startTime: {
      hour: 8, 
      minute: 50
    },
    endTime: {
      hour: 20, 
      minute: 30
    }
  }];
}

我正在尝试使用 MongoDB 聚合 $match 运算符检索班次开始和结束之间的文档,如下所示:

{
  $match: {
    'Monday.startTime.hour': {              // ex: 8
      $lte: parseInt(now.format('HH'), 10), // now hours: 18
    },
    'Monday.startTime.minute': {            // ex: 50
      $lte: parseInt(now.format('mm'), 10), // now minutes: 40
    },
    'Monday.endTime.hour': {                // ex: 20
      $gte: parseInt(now.format('HH'), 10), // now hours: 18
    },
    'Monday.endTime.minute': {              // ex: 30
      $gte: parseInt(now.format('mm'), 10), // now minutes: 40
    },
  }
}

但是问题是我们有一个如下图所示的移位示例, 第一个匹配条件:

'Monday.startTime.hour': {
  $lte: parseInt(now.format('HH'), 10),
}

将通过,因为 8 小于 18。

但第二个匹配条件匹配分钟部分:

'Monday.startTime.minute': {            // ex: 50
  $lte: parseInt(now.format('mm'), 10), // now minutes: 40
},

将失败,因为 50 大于 40

虽然在现实生活中08:50 出现在18:40 之前

【问题讨论】:

    标签: javascript node.js mongodb datetime mongoose


    【解决方案1】:

    将所有内容更改为分钟。这将帮助您解决这个问题。

    8:50 应该是 530,20:30 应该是 1230。

    当前时间为 1120(18:40)。因此,通过更改为分钟,您可以解决此问题。

    【讨论】:

      【解决方案2】:

      我能够通过首先使用 $and$or 运算符比较小时数来解决问题,请参阅下面的解释代码:

      {
        $and: [ // grouping two conditions that now time needs to be between the start and the end of the shift.
          {
            $or: [ // compare the start time hour of the shift first 
              {
                'Monday.startTime.hour': {
                  $lt: parseInt(now.format('HH'), 10),
                },
              },
              {
                $and: [ // if the upper condition didn't work will need to compare hours and minutes 
                  {
                    'Monday.startTime.hour': {
                      $lte: parseInt(now.format('HH'), 10),
                    },
                  },
                  {
                    'Monday.startTime.minute': {
                      $lte: parseInt(now.format('mm'), 10),
                    },
                  },
                ],
              },
            ],
          },
          {
            $or: [ // compare the end time hour of the shift first 
              {
                'Monday.endTime.hour': {
                  $gt: parseInt(now.format('HH'), 10),
                },
              },
              {
                $and: [ // if the upper condition didn't work will need to compare hours and minutes
                  {
                    'Monday.endTime.hour': {
                      $gte: parseInt(now.format('HH'), 10),
                    },
                  },
                  {
                    'Monday.endTime.minute': {
                      $gte: parseInt(now.format('mm'), 10),
                    },
                  },
                ],
              },
            ],
          },
        ];
      }
      

      【讨论】:

        猜你喜欢
        • 2020-06-29
        • 2019-01-24
        • 1970-01-01
        • 1970-01-01
        • 2022-06-16
        • 1970-01-01
        • 2013-05-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多