Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Robomongo, how to use custom functions?

Tags:

mongodb

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
}
like image 788
cl0udw4lk3r Avatar asked Jul 15 '13 07:07

cl0udw4lk3r


People also ask

Can we write functions in MongoDB?

You can create additional custom functions to read, insert, update, and delete data from your MongoDB using the Function Editor.

How do you run a query in Robomongo?

The query is run only when you want it run, by clicking the “play” arrow in the toolbar, typing Command/Control-Enter in the text box or pressing F5. Note that if you have not placed parentheses after a function name and hit Command/Control-Enter, Robomongo shows you the JavaScript behind that function.


1 Answers

You can access stored functions in several ways:

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}});

Note that we are using functions now, not strings as in 1)

3)

If you are using MongoDB 2.1 or above, you can do:

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

More about this: http://docs.mongodb.org/manual/tutorial/store-javascript-function-on-server/

Robomongo use the same engine that is used by MongoDB shell. Your questions is about MongoDB, not Robomongo.

like image 131
Dmitry Schetnikovich Avatar answered Oct 16 '22 05:10

Dmitry Schetnikovich