dongliyi967823 2014-07-07 06:58
浏览 452
已采纳

mysql BETWEEN等效于MongoDB

In PHP & MongoDB, i am trying to read all instances of events for a specific calendar and a specific period. Each event has start and end date. They are of MongoDate type in mongodb.

I use below condition to get the events from mongo.

$condition = array(
 'start_time'    => array('$gte'    =>  new MongoDate($start_day)), 
 'end_time'      => array('$lte'    => new MongoDate($end_day))
);

Here is my problem, i am missing the events whose start date is to my date range.

For example event starts on 01-july-2014
event ends on 10-july-2014

my mongo query - give me events from 5-july to 12-july

$condition = array(
  'start_time'    =>    array('$gte'    => '5-july-2014'), 
  'end_time'      =>    array('$lte'    => '12-july-2014')
);

When i run above mentioned event is not returned.

Any suggestions are welcome?

In mysql BETWEEN helps me to get the required results.

  • 写回答

2条回答 默认 最新

  • dpsq8476 2014-07-07 07:33
    关注

    Your query selects any events which are completely contained in the given date range, but when I understood you correctly you want events which partly or completely overlap the date-range.

    A date-range overlaps another date-range when the start of A is before the end of B and the end of A is after the start of B. So try

    'event_start'    => array('$lte'    =>  new MongoDate($search_end)), 
    'event_end'      => array('$gte'    => new MongoDate($search_start))
    

    Another pitfall I should mention is that a MongoDate doesn't represent a single day, but an exact point in time. When you omit the time, you get midnight at that date. So '$lte' => '12-july-2014' gives you 2014-04-12 0:00:00.000 which will not select anything on july 12th. When you want to include that day, either manually set a time like 12-july-2014 23:59:59 or just use the next day.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码