【问题标题】:Mongoose find with conflicting dates猫鼬发现日期冲突
【发布时间】:2019-05-19 19:48:38
【问题描述】:

我有一个 mongoose 模型,其中包含事件的开始和结束日期。

我想要一个带有开始和结束日期的查询,看看它是否与数据库中的任何事件发生冲突。

类似:

'select where' - 
    endDate < currentBooking.endDate && startDate < currentBooking.startDate) || end > currentBooking.endDate && startDate > currentBooking.startDate

但是我怎样才能把它写成一个猫鼬查询呢?

【问题讨论】:

  • Atiq Ur Rehman 直接翻译了您的查询,但为了确保您还必须包含满足此要求的事件:endDate &lt; currentBooking.endDate &amp;&amp; startDate &gt; currentBooking.startDate

标签: javascript mongodb mongoose nosql


【解决方案1】:

请测试一下,希望这能解决您的问题

Ebook.find({
  $or: [{
     $and: [{ endDate: { $lte: currentBooking.endDate } }, { startDate: { $lte: 
     currentBooking.startDate}}],
   $and: [{ endDate: { $gte: currentBooking.endDate } }, { startDate: { $gte: 
  currentBooking.startDate}}]
    }]
},function(err, data) {
        if (err)
            res.send(err);

        res.json(data);
    });
});

【讨论】:

    猜你喜欢
    • 2012-02-17
    • 2018-07-05
    • 2013-05-15
    • 2016-10-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-12
    • 1970-01-01
    • 2013-07-07
    相关资源
    最近更新 更多