首页 > 解决方案 > 考虑所有可能性,在猫鼬中的两个值之间进行查询

问题描述

所以,我不知道如何描述一个问题的问题,所以这里是一个例子:

假设我需要退房 我需要在这个给定时间预约:开始时间:8,结束时间:11

但是,这些是无法完成预约的可能性:

There's an appointment at 9 and it ends at 11; -- can't be done

There's an appointment at 8 and it ends at 10; -- can't be done

There's an appointment at 9 and it ends at 10; -- can't be done

There's an appointment at 7 and it ends at 12. -- can't be done

对于前三种可能性,我确实对如何进行查询有所了解;但是,对于最后一种可能性,我没有。我是使用猫鼬的新手,所以我真的不知道如何完成它。提前致谢。

标签: node.jsmongodbexpressmongoose

解决方案


db.collection.find({
  "$and": [
    {
      "start": {
        "$lte": 11
      }
    },
    {
      "end": {
        "$gte": 8
      }
    }
  ]
})/**8,11*/

我只是添加了基于

t1 = [8,10]

t2 = [8,11]

 isBefore = !t1.end.isBefore(t2.begin) 
                     && !t1.begin.isAfter(t2.end);

推荐阅读