dpz1983 2015-10-06 18:35
浏览 275
已采纳

MongoDB PHP聚合空结果与$ match中的日期($ lte,$ gte)

I've the following Aggregation:

$pipeline = array(
    array('$match' => array(
        'matchDate' => array(
            '$lte' => $dateEnd, // DateTime object
            '$gte' => $dateStart // DateTime object
         )
    )),
    array('$group' => array(
        '_id' => '$sport',
        'count' => array('$sum' => 1)
    ))
);

$m = new \MongoClient('localhost');
$c = $m->selectDB('test_database')->selectCollection('Match');
$t = $c->aggregate($pipeline);

This aggregation will return an empty Result.

If I run the aggregation on my MongoDB directly, it works without problems and gives the expected results. Here is the native query.

db.Match.runCommand({
  "aggregate": "Match", 
  "pipeline": [
    {
      "$match": { 
        "matchDate": {
          "$lte": new ISODate("2015-10-07T23:59:59+02:00"), 
          "$gte": new ISODate("2015-10-06T00:00:00+02:00") 
        } 
      } 
    }, 
    { 
      "$group": { "_id": "$sport", "count": { "$sum": 1 } } 
    }
]});

The problem occurs only with aggregation. Find queries with date ($lte, $gte) works also without problems.

Here is an example document.

{
  "_id": ObjectId("5613bbb79042ad801f0041ab"),
  "sport": ObjectId("5613bbb79042ad801f0041a8"),
  "matchDate": new Date("2015-09-26T13:45:00+0200")
} 

Has someone an idea whats happen here?

I'm Using

MongoDB Support 1.6.11

PHP 5.6.13

MongoDB 3.0.6

  • 写回答

1条回答 默认 最新

  • dqdfpmmi022763 2015-10-07 10:26
    关注

    It'll work with MongoDate.

    $pipeline = array(
        array('$match' => array(
            'matchDate' => array(
                '$lte' => new MongoDate($dateEnd->getTimestamp()),
                '$gte' => new MongoDate($dateStart->getTimestamp())
             )
        )),
        array('$group' => array(
            '_id' => '$sport',
            'count' => array('$sum' => 1)
        ))
    );
    
    $m = new \MongoClient('localhost');
    $c = $m->selectDB('test_database')->selectCollection('Match');
    $t = $c->aggregate($pipeline);
    

    Thanks @Blakes Seven

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)