在PHP中使用mongodb map/reduce [英] Using mongodb map/reduce in php
问题描述
我是mongodb的新手,我想在连接到mongo数据库的php代码中使用mongo map/reduce函数.
I'm new to mongodb and I want to use mongo map/reduce function in my php codes connected to my mongo database.
我有一个名为video的文档,其中包含大量项目,我想获取10个在名为"fc_total_share"的特定字段中具有最大值的项目.
I have a document named videos with a large number of items, I want to get 10 items that have the largest values in specific field named "fc_total_share".
顺便说一句,由于我的视频"文档中确实包含大量项目,您是否认为map/reduce是检索特定项目的好方法,如果不是,请帮助我找到一种更好的方法
And by the way, as my "videos" document has really large number of items, do you think that map/reduce is a good way to retrieve specific items and if not would you guys please help me to find a better way.
推荐答案
您可以使用 $ 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/zh-CN/mongodb.command.php
Just to show example Code is copied from here : http://php.net/manual/en/mongodb.command.php
这篇关于在PHP中使用mongodb map/reduce的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!