2013-07-15 78 views
10

我試圖使用MongoDB的客戶端「Robomongo」 http://robomongo.org/Robomongo,如何使用自定義函數?

它工作正常,但我不明白如何訪問到「功能」一節中創建的功能...

我要測試的MapReduce的功能,所以我創建了一個map()和減少()函數,但是當我在寫我的外殼:

db.<name_of_collection>.mapReduce(map, reduce, {out: {inline: 1}}); 

Robomongo給我下面的錯誤:

ReferenceError: map is not defined (shell):1 

我也試過這樣:

db.<collection_name>.mapReduce(db.system.js.map, db.system.js.reduce, {out: {inline: 1}}); 

但同樣,有些事情似乎是錯誤的...

uncaught exception: map reduce failed:{ 
    "errmsg" : "exception: JavaScript execution failed: ReferenceError: learn is not defined", 
    "code" : 16722, 
    "ok" : 0 
} 

回答

19

您可以訪問多種方式存儲功能:

1)

db.collection.mapReduce(
    "function() { return map(); }", 
    "function(key, values) { return reduce(key, values); }", 
    {out: {inline: 1}}); 

2)

db.collection.mapReduce(
    function() { return map(); }, 
    function(key, values) { return reduce(key, values); }, 
    {out: {inline: 1}}); 

注意,我們現在使用的功能,而不是字符串,如1)

3)

如果您正在使用的MongoDB 2.1以上,你可以這樣做:

db.loadServerScripts(); 
db.collection.mapReduce(
    map, 
    reduce, 
    {out: {inline: 1}});  

關於此的更多信息: http://docs.mongodb.org/manual/tutorial/store-javascript-function-on-server/

Robomongo使用MongoDB shell使用的引擎。你的問題是關於MongoDB,而不是Robomongo。

9

使用RoboMongo, 在shell命令文本框中輸入創建功能後:

db.loadServerScripts(); 
myFunctionName(); 

,並單擊工具欄中的按鈕Execute

相關問題