我是mongodb的新手,我想在连接到我的mongo数据库的php代码中使用mongo-map/reduce函数。
我有一个名为videos的文档,其中包含大量项目,我想在名为"fc_total_share"的特定字段中获得10个值最大的项目。
顺便说一句,由于我的"视频"文档中有大量的项目,你认为map/reduce是检索特定项目的好方法吗?如果不是,请你们帮我找到更好的方法。
您可以使用$db->command()来完成此操作
<?php
// sample event document
$events->insert(array("user_id" => $id,
"type" => $type,
"time" => new MongoDate(),
"desc" => $description));
// construct map and reduce functions
$map = new MongoCode("function() { emit(this.user_id,1); }");
$reduce = new MongoCode("function(k, vals) { ".
"var sum = 0;".
"for (var i in vals) {".
"sum += vals[i];".
"}".
"return sum; }");
$sales = $db->command(array(
"mapreduce" => "events",
"map" => $map,
"reduce" => $reduce,
"query" => array("type" => "sale"),
"out" => array("merge" => "eventCounts")));
$users = $db->selectCollection($sales['result'])->find();
foreach ($users as $user) {
echo "{$user['_id']} had {$user['value']} sale(s).n";
}
?>
只是为了展示从这里复制代码的示例:http://php.net/manual/en/mongodb.command.php