2012-01-30 123 views
0

我想要做一個模式匹配並替換我的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 
) 

回答

1

典型地,爲了測試此最簡單的方法是簡單地從外殼運行M/R。這將有助於編譯b/c shell可以識別錯誤的語法。

如果我使用我的「人工編輯」技能,以下看起來是錯誤的。

s = s.replace(/\/\"/i,"/""); 

你逃避/"/代替它?看看"/"",這似乎是一個太多的雙引號。