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 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化