如何使用php查询mongodb日期 [英] How to query mongodb Date using php

查看:817
本文介绍了如何使用php查询mongodb日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将此查询从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)));

请参见 http://cookbook.mongodb.org/patterns/date_range/

I need to convert this query from php to mongoDB query

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

I have date stored in variable

$date = "2013-09-02";

and in my mongo Document the date sorted as :

$dateAdded = new MongoDate(strtotime('2013-09-02 12:21:55'));

I tried

$date = new MongoDate(strtotime("$date"));

$mongo->find(array('date_added'=>array('$lt'=>$date)));

and 
$mongo->find(array('date_added'=>$date));

but without success .

so I need to query usin (Y-m-d) not (Y-m-d h:i:s) so how to use LIKE query for data in mongo

Thanks

解决方案

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/

这篇关于如何使用php查询mongodb日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆