如何将MongoDB聚合管道转换为PHP?

时间:2014-10-07 17:28:16

标签: php mongodb laravel

我正在使用以下MongoDB查询(并且它正在使用RoboMongo),返回一个包含30个元素的数组,每个元素对应于$ match元素上指定的日期范围:

db.alcobaca.aggregate( [
        { $project:
            { time:1,
              temp:1,
                frio:
                {
                    $cond:
                    [{ $lte: [ "$temp", 7.2 ] }, 0.25, 0 ]}}},
        {
            $match: {
                time: {
                    $gte: new Date('11/01/2011'),
                    $lt: new Date('11/30/2011')
                }
            }
        },
        {
            $group: {
                _id: {

       ord_date: {
           day: {$dayOfMonth: "$time"},
           month: {$month: "$time"},
           year: {$year: "$time"}
       }
    },
                horasFrio: { $sum: '$frio' }
            }
        },
        { $sort: { '_id.ord_date': 1} }
    ])

当我翻译成PHP,在成功连接到数据库之后使用Laravel框架(我已经使用另一个更简单的查询测试了它):

$c->aggregate (
  [
    ['$project' =>
       ['time' => 1,
        'temp' => 1,
        'frio' =>
          ['$cond' =>
              [['$lte' => ['$temp', 7.2]], 0.25, 0]]]],
    ['$match' => [
        'time' => [
          '$gte' => new DateTime('11/01/2011'),
          '$lt'  => new DateTime('11/03/2011')
        ]
      ]
    ],
    [
      '$group' => [
        '_id'       => [
          'ord_date' => [
            'day'   => ['$dayOfMonth' => '$time'],
            'month' => ['$month' => '$time'],
            'year'  => ['$year' => '$time']
          ]
        ],
        'horasFrio' => ['$sum' => '$frio']
      ]
    ],
    ['$sort' => ['_id.ord_date' => 1]]
  ]
);

它返回一个空数组。我错过了一些语法问题吗?

谢谢

0 个答案:

没有答案