[英]Using mongodb map/reduce in php
I'm new to mongodb and I want to use mongo map/reduce function in my php codes connected to my mongo database. 我是mongodb的新手,我想在连接到mongo数据库的php代码中使用mongo map / reduce函数。
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". 我有大量项目的命名视频文件,我想获得10项有一个名为“fc_total_share”特定领域最大的价值。
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. 顺便说一句,由于我的“视频”文档中确实包含大量项目,您是否认为map / reduce是检索特定项目的好方法,如果不是,请帮助我找到一种更好的方法。
You can do this using $db->command() 您可以使用$ 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";
}
?>
Just to show example Code is copied from here : http://php.net/manual/en/mongodb.command.php 只是为了显示示例代码,是从此处复制的: http : //php.net/manual/zh/mongodb.command.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.