0

我正在尝试使用官方 PHP 驱动程序和组(http://www.php.net/manual/en/mongocollection.group.php)函数将工作MongoDB组查询转换为等效查询。PHP

这是工作的原始 MongoDB 查询:

db.collection.group(
{
    keyf: function(doc) {
        var date = new Date(doc.executed);
        var dateKey = (date.getMonth() + 1) + "/" + date.getDate() + "/" + date.getFullYear();
        return {'day': dateKey};
     },
    cond: { executed: { $gt: new Date('01/01/2013') }},
    initial: { executions:0 },
    reduce: function(obj, prev) { prev.executions++; }
});

这是我不工作的PHP 代码:

$keyf = new MongoCode('function(doc) {
    var date = new Date(doc.executed);
    var dateKey = (date.getMonth() + 1) + "/" + date.getDate() + "/" + date.getFullYear();
    return {\'day\': dateKey};
}');

$initial = array("executions" => 0);
$reduce = "function(obj, prev) { prev.executions++; }";
$conditionals = array("executed" => array('$gt' => "new Date('01/01/2013')"));

$result = $c->group($keyf, $initial, $reduce, array("condition" => $conditionals));

和:print_r()_$result

Array
(
    [retval] => Array
        (
        )

    [count] => 0
    [keys] => 0
    [ok] => 1
)

谢谢!

4

1 回答 1

1

您正在condition将 a 与字符串而不是日期进行比较。你需要一个MongoDate代替。就像是:

$cond_date = new MongoDate(strtotime("2013-01-01 00:00:00"));
$conditionals = array("executed" => array('$gt' => $cond_date));
于 2013-01-30T22:55:28.277 回答