PHP MongoDB映射减少数据库断言失败

2024-03-29

我第一次使用 PHP/MongoDB 进行 Map/Reduce。运行 MapReduce 命令时遇到错误。

My code:

$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 断言代码 13111
errmsg 数据库断言失败
好的 0

我非常仔细地遵循了这个例子here http://php.net/manual/en/mongodb.command.php所以我不确定出了什么问题。 任何建议表示赞赏。


重要的是要记住,在使用 mapReduce() 时,reduce() 函数的返回值需要与您期望在 reduce() 函数的“values”元素中获得的形状相同——而且,反过来,需要与 emit() 函数发出的形状相同。

给定一个包含以下形式文档的集合:

> db.posts.count();
1000
> db.posts.findOne();
{
    "_id" : 0,
    "date_posted" : ISODate("2012-04-04T05:54:44.535Z"),
    "topic_id" : "sierra"
}

以下代码将产生您想要的输出:

<?php

$conn = new Mongo("localhost:$port");
$db = $conn->test;
$collection = $db->tst;

$map = new MongoCode(
    "function() { emit( this.topic_id, { last_post: this.date_posted } ); }"
);

$reduce = new MongoCode(
  "function(key, values) { ".
      "var max = ISODate('1970-01-01T00:00:00Z'); ".

      "values.forEach(function(val) { ".
          "if ( max < val.last_post ) max = val.last_post; ".
      "}); ".

      "return {last_post : max}; " .
  "}"
);


$result = $db->command( array(
    "mapreduce" => "posts",
    "map" => $map,
    "reduce" => $reduce,
    "query" => array( "topic_id" => "alpha"), 
    "out" => array( "merge" => "lastPosts")
    )
);
echo "result: "; print_r($result);

$collection = $db->lastPosts;
$cursor = $collection->find()->limit(6);

date_default_timezone_set("UTC");
foreach( $cursor as $doc ) {
    $date = date( "r", $doc['value']['last_post']->sec );
    echo $doc['_id'] . " last visited at " . $date ."\n" ;
}

?>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

PHP MongoDB映射减少数据库断言失败 的相关文章

随机推荐