在MongoDB中替换()MapReduce映射函数
问题描述:
我想要做一个模式匹配并替换我的MongoDB mapReduce。我在数据库中映射了推文的来源。并获得重复的结果,如在MongoDB中替换()MapReduce映射函数
1 - web has 38867
2 - <a href="http://www.tweetdeck.com" rel="nofollow">TweetDeck</a> has 23873
3 - <a href="http://www.hootsuite.com" rel="nofollow">HootSuite</a> has 10696
4 - <a href="http://twitterfeed.com" rel="nofollow">twitterfeed</a> has 9562
5 - <a href="http://twitter.com/#!/download/iphone" rel="nofollow">Twitter for iPhone</a> has 6818
6 - <a href="http://www.echofon.com/" rel="nofollow">Echofon</a> has 5869
7 - <a href="http://www.tweetdeck.com/" rel="nofollow">TweetDeck</a> has 5497
#2和#7唯一的区别是“.com /”和“.com”在href。我想在我的map函数中进行模式匹配,但是我收到了编译错误。我可能会迷失在翻译层面。
PHP ==> Mongo ==> javascript。
这是我的代码块
$map = 'function() {
if (!this.source) {
return;
}
s = this.source;
s = s.replace(/\/\"/i,"/"");
emit(s,1);
}';
$reduce = "function(previous, current) {
var count = 0;
for (index in current) {
count += current[index];
}
return count;
}";
$mapFunc = new MongoCode($map);
$reduceFunc = new MongoCode($reduce);
$collectionOutName = 'mrTweetSource';
$mr = $db->command(array(
'mapreduce' => 'tweet',
'map' => $mapFunc,
'reduce' => $reduceFunc,
'out'=>$collectionOutName));
结果是
(
[assertion] => couldn't compile code for: _map
[assertionCode] => 13598
[errmsg] => db assertion failure
[ok] => 0
)
答
典型地,为了测试此最简单的方法是简单地从外壳运行M/R。这将有助于编译b/c shell可以识别错误的语法。
如果我使用我的“人工编辑”技能,以下看起来是错误的。
s = s.replace(/\/\"/i,"/"");
你逃避/"
与/
代替它?看看"/""
,这似乎是一个太多的双引号。