Running functions that are stored in MongoDB?


#1

I want to store functions in MongoDB (don’t ask why, I just want to).

If I go into meteor mongo and do:

db.myCollection.insert({ "myFunc": function(num1, num2) { return num1 + num2 } })

I can retrieve my last insert and directly run the function stored on it quite easily and naturally:

db.myCollection.find().sort({$natural:-1})[0].myFunc(10,5)

returns 15 in meteor mongo

How do I do this equivalent code in Meteor’s server-side or client-side code? When I go to retrieve the document and the myFunc property, it returns the function as a string, in both the server and client code. I’ve read that eval is not a good way to run functions that are in the form of strings.


#2

I’m also looking for a clean solution to do that (and I don’t understand why it’s possible in ‘meteor mongo’ but not in the Meteor app).


#3

Meteor applications use Minimongo. Minmongo works with documents in EJSON format, which in turn relies on JSON. Neither format supports functions. You’ll have to come up with a custom solution for handling this (like using eval (very carefully), loading stored function strings into a custom function builder that you’ve created, etc.).


#4

Thank you very much!