How to translate MongoDB aggregation pipeline to PHP?

I use the following MongoDB query (and it works with RoboMongo), returning an array of 30 elements, one for each day of the date range specified in the $ match element:

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} }
    ])

      

When I was translating to PHP while working on a Laravel database, after successfully connecting to the database (I already tested it with another simpler query):

$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]]
  ]
);

      

It returns an empty array. Am I missing any syntax problem?

thank

+3


source to share





All Articles