Mongo User Defined Functions and Map Reduce

北慕城南 提交于 2019-12-19 10:39:49

问题


Is there a way in mongo to create user-defined Javascript functions. I have several Map/Reduce functions on the client side that i would like to use within other MR functions.

For example, several MR functions calculate all sorts of averages. I want to be able to use them like so :

function reduce(k,v)
{
    if (val > myDatabaseAverage())
    // ..do something
}

回答1:


Use db.system.js.save( { _id : "myDatabaseAverage" , value : function(){ // ..do something } } );

That will store the JS function on the server and can be accessed by m/r from that point on.

For further examples see : https://github.com/mongodb/mongo/blob/master/jstests/storefunc.js




回答2:


As mentioned by @Remon van Vliet,

You will have to use,

db.system.js.save( { _id : "myDatabaseAverage" , 
                     value : function(){ // ..do something } 
                 } );

to save your function first,

and then you need to call,

db.loadServerScripts();

before you execute your function.



来源:https://stackoverflow.com/questions/6491769/mongo-user-defined-functions-and-map-reduce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!