Hi I'm having trouble deciding on the correct way to store my objects in the monogoDB database, from node.js.
A Strip down example of my classes are shown below, in reality it is much more complex:
function Object1{
var variables;
var object2:
var object3:
functions
...
}
function Object2{
var variables;
var object4;
var object5;
...
functions
...
}
...
etc
A typical function call in Object1 would result in several calls to other objects, and objects belonging to them objects.
If I store object1 in mongoDB, I cannot store the functions. From my research I have two options:
Change objects prototype when the object is loaded, as per this answer
Question: I would then be required, to change the prototypes for every subsequent object, but considering the large amount of subsequent objects, this does not seem like the right thing to do, is there an easy way to do this?
Use mongoose schemas to define the objects allowing me to use virtuals to store the functions as in this example.
Which would result in the following:
var objectSchema = new Schema({ variables: Type object2: Schema.ObjectId }); virtual functions ...Question: In this case, each time I wanted to access variables, or call virtual functions in other objects whose ObjectId is in the schema, I would have to call find() on the database. This would potentially result in a large amount of database calls compared to having one database call that loads the object and passing the information round to the subsequent functions, which makes this option also seem invalid?
Question: Is there a better way than these two methods?