我需要将此查询从php转换为mongoDB查询
$query = "select * from table where data_added like '%data%';
我把日期存储在变量中
$date = "2013-09-02";
在我的mongo文档中,日期排序为:
$dateAdded = new MongoDate(strtotime('2013-09-02 12:21:55'));
我试过了
$date = new MongoDate(strtotime("$date"));
$mongo->find(array('date_added'=>array('$lt'=>$date)));
and
$mongo->find(array('date_added'=>$date));
但没有成功.
所以我需要查询我们(Y-m-d)而不是(Y-m-d h:i:s)
那么如何在mongo中使用LIKE查询数据
谢谢
解决方法:
您需要进行范围查询.创建一个时间戳,例如使用strtotime(),以获取当天开始时的unix时间戳,以及另一个时间戳和当天结束时间.
根据您是希望这两个端点包含还是独占,您可以使用
// Both points/seconds inclusive
->find(array("date" => array('$gte' => $startOfDay, '$lte' => $endOfDay)));
// Both seconds exclusive
->find(array("date" => array('$gt' => $startOfDay, '$lt' => $endOfDay)));
标签:php,mongodb
来源: https://codeday.me/bug/20190629/1325087.html