Mithun Satheesh
Mithun Satheesh

Reputation: 27845

How to store JavaScript functions into documents in mongoose?

I need to store some data and associated functions into the mongodb document.

{name:"data1",event:function(){ /* some code */ }}
{name:"data2",event:function(){ /* some other code */ }}

i am using mongoose.js ORM for my project. How should i write my schema for this?

quoting from the mongoose guide

The permitted SchemaTypes are String, Number, Date, Buffer, Boolean, Mixed, ObjectId and Array.

But if you check with the datatypes allowed by mongodb, you can find ways to store js function as mentioned in mongo-db-data-types-article

Code class a special class that let’s you store javascript functions in Mongo DB, can also provide a scope to run the method in

I couldnt find the above datatype on mongoose. Any ideas?

Upvotes: 1

Views: 4240

Answers (1)

aaronheckmann
aaronheckmann

Reputation: 10780

This module adds function support to Mongoose: https://github.com/aheckmann/mongoose-function

Upvotes: 1

Related Questions