2011-11-30 81 views
0

我正在使用1.15M文檔在集合上運行mapreduce。在重複的情況下,當我返回結果時,我希望優先考慮某些文檔值(即,一個文檔中的字段值可能比另一個文檔中的字段值更精確,但可能根本不存在)。MongoDB:在Mapreduce期間未定義函數

要選擇一個值,我定義這個函數(參數是在最優選的是最不喜歡的順序):

function getPreferredValue() { 
var length = arguments.length; 
for (var i = 0; i < length; i++) { 
    if (arguments[i] != null) { 
     return arguments[i]; 
    } 
} 
return null; 
} 

我MapReduce的命令如下:

db.runCommand({ 
mapreduce:"catalog", 
query: {$or:[ 
    {name:/^HIP/i}, 
    {name:/^SAO/i} 
]}, 
map:function() { 
    emit({dummy:this.xref}, this); // give me the whole damn object then 
}, 
reduce:function(key, values) { 
    var tempObject = {hipObject: null, saoObject:null}; 
    for (var i = 0; i < values.length; i++) { 
     if (values[i].name.search("/^HIP") != -1) { 
      var hipObject = {bii: null, lii: null, class: null, radeg: null, decdeg: null, rapm:null, decpm:null, vmag:null, parallax:null, xref:null}; 
      hipObject.bii = values[i].bii; 
      hipObject.lii = values[i].lii; 
      hipObject.class = values[i].class; 
      hipObject.radeg = values[i].radeg; 
      hipObject.decdec = values[i].decdeg; 
      hipObject.rapm = values[i].rapm; 
      hipObject.decpm = values[i].decpm; 
      hipObject.vmag = values[i].vmag; 
      hipObject.parallax = values[i].parallax; 
      hipObject.xref = values[i].xref; 
      tempObject.hipObject = hipObject; 
     } 
     if (values[i].name.search("/^SAO") != -1) { 
      var saoObject = {bii: null, lii: null, class: null, radeg: null, decdeg: null, rapm:null, decpm:null, vmag:null, parallax:null, xref:null}; 
      saoObject.bii = values[i].bii; 
      saoObject.lii = values[i].lii; 
      saoObject.class = values[i].class; 
      saoObject.radeg = values[i].radeg; 
      saoObject.decdec = values[i].decdeg; 
      saoObject.rapm = values[i].rapm; 
      saoObject.decpm = values[i].decpm; 
      saoObject.vmag = values[i].vmag; 
      saoObject.parallax = values[i].parallax; 
      saoObject.xref = values[i].xref; 
      tempObject.saoObject = saoObject; 
     } 
    } 

    var result = {bii: null, lii: null, class: null, radeg: null, decdeg: null, rapm:null, decpm:null, vmag:null, parallax:null, xref:null} 
    result.bii = getPreferredValue(tempObject.hipObject.bii, tempObject.saoObject.bii); 
    result.lii = getPreferredValue(tempObject.hipObject.lii, tempObject.saoObject.lii); 
    result.class = getPreferredValue(tempObject.hipObject.class, tempObject.saoObject.class); 
    result.radeg = getPreferredValue(tempObject.hipObject.radeg, tempObject.saoObject.radeg); 
    result.decdeg = getPreferredValue(tempObject.hipObject.decdeg, tempObject.saoObject.decdeg); 
    result.rapm = getPreferredValue(tempObject.hipObject.rapm, tempObject.saoObject.rapm); 
    result.decpm = getPreferredValue(tempObject.hipObject.decpm, tempObject.saoObject.decpm); 
    result.vmag = getPreferredValue(tempObject.hipObject.vmag, tempObject.saoObject.vmag); 
    result.parallax = getPreferredValue(tempObject.hipObject.parallax, tempObject.saoObject.parallax); 
    result.xref = tempObject.hipObject.xref; 

    return result; 
}, 
out: {replace:"catalog2", db:"astro3"} 
}); 

不幸的是,當試圖運行時,我得到一個錯誤,getPreferredValue沒有被定義。

可能是一個愚蠢的錯誤在我的部分,但任何人都可以幫助? Jason

+0

你應該刪除這個問題呢。 – Petrogad

回答

0

無視,我沒有保存功能服務器端。

0

您也可以存儲在地圖的功能,減少命令的「範圍」與

scope: { 
    getPreferredValue: function(){...} 
} 
相關問題