mongodb function from rails

杀马特。学长 韩版系。学妹 提交于 2020-01-05 04:33:11

问题


I use mongomapper with mongodb for rails models. In the mongodb I have this function

db.system.js.save({_id:'resumenTemporada',value:function(collection, condition){
    var res=
    db[collection].group({     
        key:{},
        reduce:function(obj,prev){      
            prev.ppa += obj.precipitation;  
            if( obj.temperature < 5 && obj.temperature >0) prev.hf += obj.temperature/4;
        },
        initial : {ppa:0,hf:0},               
        cond: condition});     
    return res ;
    } 
}); 

And get this output

> db.eval("return resumenTemporada('stations',{nombre:'pua',fecha:{$gt:'2011-01-01'}});")
[
        {
                "ppa" : 241.19999999999982,
                "hf" : 743.2295249999986
        }
]

Ok, how I can call this function from rails ?


回答1:


try:

results = MongoMapper.database.eval('resumenTemporada', 'stations',{nombre:'pua',fecha:{$gt:'2011-01-01'}})

More examples here: https://gist.github.com/307753



来源:https://stackoverflow.com/questions/6084584/mongodb-function-from-rails

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