How to query mongodb Date using php

后端 未结 1 1427
萌比男神i
萌比男神i 2021-01-06 07:48

I need to convert this query from php to mongoDB query

$query = \"select * from table where data_added like \'%data%\';

I have date stored

1条回答
  •  醉话见心
    2021-01-06 08:36

    You need to do a range query. Create a timestamp, for example using strtotime(), to get the unix timestamp at the start of the day, and another one and the end of the day.

    Depending on if you want these two ends inclusive or exclusive, you then use

    // Both points/seconds inclusive
    ->find(array("date" => array('$gte' => $startOfDay, '$lte' => $endOfDay)));
    // Both seconds exclusive
    ->find(array("date" => array('$gt' => $startOfDay, '$lt' => $endOfDay)));
    

    See http://cookbook.mongodb.org/patterns/date_range/

    0 讨论(0)
提交回复
热议问题