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

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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)