简体   繁体   English

在mongoDB中存储带有函数的对象和其他对象

[英]Storing objects with functions and other objects in mongoDB

Hi I'm having trouble deciding on the correct way to store my objects in the monogoDB database, from node.js. 嗨,我在决定从node.js将对象存储在monogoDB数据库中的正确方法时遇到了麻烦。

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. Object1中的典型函数调用将导致对其他对象以及属于它们的对象的多次调用。

If I store object1 in mongoDB, I cannot store the functions. 如果将object1存储在mongoDB中,则无法存储函数。 From my research I have two options: 根据我的研究,我有两种选择:

  1. 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? 问题 :然后,我将需要为每个后续对象更改原型,但是考虑到大量后续对象,这似乎不是正确的选择,是否有简便的方法?

  2. 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. 问题:在这种情况下,每次我想访问变量或在其对象ID处于架构中的其他对象中调用虚拟函数时,都必须在数据库上调用find()。 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? 问题:是否有比这两种方法更好的方法?

I've managed to solve my problem (i think) by removing the function definitions from the objects and then passing an instance of the object to the function. 我设法通过从对象中删除函数定义,然后将对象的实例传递给函数来解决我的问题。

object1.js object1.js

module.exports = {
    //Create an object
    Object1: function(params, object2, object3){
            this.variables = params;
            this.object2 = object2:
            this.object3 = object3:
    },
    //object function
    myFunction: function(object1, params){
            do stuff with object1.
    }
}

In my code I then use something like: 然后在我的代码中使用类似以下内容的代码:

app.js app.js

var object1 = require('./object1.js');
var myObject = new object1.Object1();

object1.myFunction(myObject, params);

instead of: 代替:

app.js app.js

var object1 = require('./object1.js');
var myObject = new object1.Object1();

object1.object1Function(params);

Instances of Object1 can then be stored in the database, and the functions can be accessed throughout the application by requiring the module. 然后可以将Object1的实例存储在数据库中,并且可以通过需要该模块在整个应用程序中访问这些功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM