gpt4 book ai didi

php - MongoDB,errmsg,异常 : couldn't compile code for: _map, PHP,命令,MapReduce

转载 作者:行者123 更新时间:2023-11-30 18:16:53 25 4
gpt4 key购买 nike

这里是 MongoDB Map Reduce 的新手...但是找不到任何关于这里出了什么问题的文档。无论如何 - 这是一个示例文档:

数组( [_id] => MongoId 对象 ( [$id] => 5076e95791fa5e4c69002ec3 )

[dataProviders] => Array
(
[0] => Array
(
[key] => facebook
[data] => Array
(
[id] => 100001583260053
[name] => MyName
[link] => http://www.facebook.com/Mynameagain
[username] => MyName
[birthday] => 02/19/1959
[hometown] => Array
(
[id] => 104022969633581
[name] => MyName
)

[location] => Array
(
[id] => 104022969633581
[name] => MyHome
)

[bio] => MyBio
[work] => Array
(
[0] => Array
(
[employer] => Array
(
[id] => 119738361373063
[name] => .....
)

[start_date] => 0000-00
[end_date] => 0000-00
)

)

[education] => Array
(
[0] => Array
(
[school] => Array
(
[id] => 108891959147272
[name] => ....
)

[type] => High School
)

[1] => Array
(
[school] => Array
(
[id] => 133940149955590
[name] => .....
)

[type] => College
)

)

[gender] => female
[email] => MyEmail@myemail.com
[timezone] => 2
[locale] => US
[languages] => Array
(
[0] => Array
(
[id] => 106412222728952
[name] => MyName
)

)

[verified] => 1
[updated_time] => 2012-08-10T17:33:16+0000
[avatarUrl] => https://graph.facebook.com/1000/picture
[avatarUrlOriginal] => https://graph.facebook.com/1000/picture?type=large
[avatarUrlLarge] => https://graph.facebook.com/1000/picture
)

)

)

我的问题是,我正在尝试运行一个基本的 MapReduce,以仅从该组中拉取 Facebook 用户。基本上我想做一个非常简单的计数。据我所知,这应该有效,但我收到一条错误消息:

error_reporting(E_ALL);
ini_set('display_errors', '1');
include "phpmapreduce/lib/MongoMapReduce.php";
include "phpmapreduce/lib/MongoMapReduceResponse.php";
include "phpmapreduce/lib/XMongoCollection.php";

$collection_name = "users";

$map = "function(){emit({source: this.dataProviders.0.key}, {count: 1}); }}";

$reduce = "function(key, values){
var sum= {count: []};
for(var i in values)
sum += values[i];
return sum;
}";
$query = array('dataProviders.0.key' => 'facebook');

echo "Here's our map Function! <br/>";
$mapFunc = new MongoCode($map);
echo "<pre>";
print_r($mapFunc);
$reduceFunc = new MongoCode($reduce);
print_r($reduceFunc);
$command= array(
"mapreduce"=>'texasHoldem.users',
"map"=>$mapFunc,
"reduce"=>$reduceFunc,
"query" =>$query,
"out" => array(
"inline" => 1)
);
try {
$mongo = new Mongo('mongodb://1.2.3.4.5.6.7./mydb', array("replicaSet" => true));
}
catch(MongoConnectionException $e) {
$msg = "Error Connecting to MongoDB" . "<br/>";
die($msg);
}
echo "Connected" . "<br/>";
$db = $mongo->selectDb('yallapop');

echo "got mydb!" . "<br/>";

$db->setSlaveOkay(); // This line is needed to be able to read from slave
$collection= $db->selectCollection('texasHoldem.users'); // Users collection
echo "Got the Collection" . "<br/>";
echo "setting timeout" . "<br/>";
//$cursor = $collection->find()->timeout(30000);
MongoCursor::$timeout = -1;
echo "<pre>";
echo "let's do this! Running command" . "<br/>";

$statsInfo = $db->command($command);
print_r($statsInfo);

SO this works well up to this point:
Connected
got myDB!
data established
setting timeout
Got the Collection
setting timeout

let's do this! Running command
Array
(
[errmsg] => exception: couldn't compile code for: _map
[code] => 13598
[ok] => 0
)

我在任何地方都找不到关于这个特定错误的任何信息。 _map 函数无法编译。以前没见过这个。还有谁有经验吗?它使用 MongoDB 和 PHP 5.3。谢谢!

最佳答案

看起来你的代码中有几个语法错误,这就是它无法编译的原因。

在你的 map 函数中,你有太多的右花括号,而且 emit 键也不应该是一个文档。

$map = "function() {
emit( this.dataProviders.0.key, {count: 1});
}";

reduce函数中,如果只是求和,sum中不需要数组。只是做:

$reduce = "function(key, values) {
var sum = {count: 0};
for (var i in values) {
sum.count += values[i].count;
}
return sum;
}";

尝试一下是否适合您。

关于php - MongoDB,errmsg,异常 : couldn't compile code for: _map, PHP,命令,MapReduce,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13039744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com