问题
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