10

I'm trying to use the mongodb client "Robomongo" http://robomongo.org/

It works fine, but I can't understand how to access to the functions created on the "functions" section...

I want to test the mapReduce functionality, so I've created a map() and reduce() function, but when I write on my shell:

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

Robomongo give to me the following error:

ReferenceError: map is not defined (shell):1

I've also tried like this:

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

But again, something seems to be wrong...

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

2 回答 2

20

您可以通过多种方式访问​​存储的函数:

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。

于 2013-07-16T14:52:27.977 回答
10

使用 创建函数后RoboMongo,在 shell 命令文本框中输入:

db.loadServerScripts();
myFunctionName();

并单击Execute工具栏中的按钮

于 2016-10-06T20:36:51.837 回答