Running functions that are stored in MongoDB?

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:


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.

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).

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.).

Thank you very much!