mongodb function from rails
Asked Answered
P

1

1

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 ?

Parrakeet answered 21/5, 2011 at 21:31 Comment(0)
Y
1

try:

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

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

Yield answered 22/5, 2011 at 1:20 Comment(1)
result = MongoMapper.database.eval("return resumenTemporada('stations',{nombre:'pua',fecha:{$gt:'2011-01-01'}});")Parrakeet

© 2022 - 2024 — McMap. All rights reserved.