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日付ではなく文字列と比較しています。代わりに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 に答える