php mongo客户端获得昨天recrods

时间:2017-07-18 09:35:33

标签: php mongodb

有人可以告诉我如何使用PHP Mongo客户端类在Mongo数据库中获取昨天的记录。

这是我尝试的但结果是0(昨天的日期已经有记录),我想我做错了。

$date = date('Y-m-d', strtotime("-1 days"));
$fromDate = $date." 00:00:00";
$toDate   = $date." 23:59:59";

$filters  = array(
                'orderDate'=>array(
                            '$lt'=>$fromDate,
                            '$gte'=>$toDate,
                       )
               );
$records  = $transCollec->find($filters);
echo $records->count(); //0

更新

我在mongo中以这种方式保存订单日期:

//This date is comming from third party
$transactionDate = new Zend_Date($transactionApi["Stat"]["datetime"], 'yyyy-MM-dd HH:mm:ss', 'en');
//this will save into mongodb
$transaction['orderDate'] = $transactionDate->toString("yyyy-MM-dd HH:mm:ss");
orderDate=date("Y-m-d H:i:s")

1 个答案:

答案 0 :(得分:1)

基于发布的问题,立即引人注目的是,您正在尝试查找前一天的记录,但您的条件不匹配,$lt$gte的值必须为交换,如:

$date = date('Y-m-d', strtotime("-1 days"));
$fromDate = $date." 00:00:00";
$toDate   = $date." 23:59:59";

$filters  = array(
    'orderDate'=> array(
        '$lt' => $toDate, //changed
        '$gte' => $fromDate, //changed
    )
);
$records  = $transCollec->find($filters);
echo $records->count();