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 ?

1 Answer 1

1

try:

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

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

Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.