我第一次使用 PHP/MongoDB 进行 Map/Reduce。运行 MapReduce 命令时遇到错误。
我的代码:
$map = "function () {".
"emit(this.topic_id, {re_date:this.date_posted}); " .
"}";
$reduce = "function (key, values) {" .
"var result = {last_post: 0};" .
"var max = ISODate('1970-01-01T00:00:00Z');" .
"values.forEach(function (value) {" .
"if (max == ISODate('1970-01-01T00:00:00Z')) {" .
"max = value.re_date;}" .
"if (max < value.re_date) {max = value.re_date;}});" .
"result.last_post = max;" .
"return result;" .
"}";
$mapFunc = new MongoCode($map);
$reduceFunc = new MongoCode($reduce);
$collection = new MongoCollection($db, 'posts');
$command = array(
'mapreduce' => $collection,
'map' => $mapFunc,
'reduce' => $reduceFunc,
"query" => array("topic_id" => "2116"),
"out" => array("merge" => "eventCounts"));
$threadInfo = $db->command($command);
$threadsCollection = $db->selectCollection($threadInfo['result']);
$stats = $statsCollection->find();
foreach ($stats as $stat) {
echo $stats['_id'] .' Visited ';
foreach ($stats['value']['types'] as $type => $times) {
echo "Type $type $times Times, ";
}
foreach ($stats['value']['tags'] as $tag => $times) {
echo "Tag $tag $times Times, ";
}
echo "\n";
}
当我运行命令时,我得到了错误:
字段的断言类型错误(mapreduce)3!= 2 assertionCode 13111
errmsg db 断言失败
ok 0
我非常密切地关注了这里的示例,所以我不确定出了什么问题。任何建议表示赞赏。