在MongoDB中存储和检索JavaScript对象 [英] Storing and retrieving JavaScript objects in/from MongoDB

查看:107
本文介绍了在MongoDB中存储和检索JavaScript对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用node-mongo-native驱动程序来处理node.js和MongoDB.

I am currently playing around with node.js and MongoDB using the node-mongo-native driver.

我使用Mongo控制台存储和检索JS对象进行了一些测试.我发现,如果我存储的对象包含函数/方法,则方法和函数也将存储在集合中.这很有趣,因为我认为功能无法存储在MongoDB中(如

I tested a bit around using the Mongo console storing and retrieving JS objects. I figured out, that if I store an object that contains functions/methods the methods and functions will also be stored in the collection. This is interesting since I thought that functions could not be stored in MongoDB (with the exception of the system.js collection, as suggested by the Mongo docs).
Also it will not only store the methods but actually each method and member of the object's entire prototype chain. Besides that I dont like this behaviour and think it's unintuitive I mustn't have it.

我本来要管理Mongo集合中的用户.为此,我有一个User对象,其中包含所有用户方法,这些方法用作用户每个实例的原型.用户对象本身仅包含用户属性.

I was going to manage users in a Mongo collection. To do this I have a User object containing all of the users methods functioning as a prototype for each instance of an user. The user object itself would only contain the users attributes.

如果我将用户存储在Mongo集合中,则只想存储用户对象的自身属性.没有原型成员,尤其是没有原型方法.目前,我不知道如何清洁地执行此操作.我认为可能的选项是:

If I store a user in the Mongo collection I only want to store the own properties of the user object. No prototype members and especially no prototype methods. Currently I do not see how to cleanly do this. The options that I figured might work are:

  1. 使用foreach和hasOwnProperty创建浅表副本并将其存储在集合中.
  2. 向每个用户添加一个数据属性,该数据属性包含对象的所有属性,并且可以存储在集合中.
  3. 这就是我写这篇文章的初衷:我还可以将所有原型属性设置为不可枚举,应该阻止它们存储在集合中.
  1. creating a shallow copy using foreach and hasOwnProperty and storing this copy in the collection.
  2. Add a data attribute to each user that contains all the object's attributes and can be stored in the collection.
  3. This just came to my mind writing this: I could also set all the prototypes properties to not enumerable which should prevent them from being stored in the collection.

但是,相反,我确实有相同的问题:从集合中加载用户时. AFAIK创建后无法更改JavaScript中的对象原型.而且,当Mongo实例化从集合中检索到的对象时,也没有办法指定要使用的原型.因此,基本上,我总是使用Mongo获得从Object继承的对象.据我所知,从此时开始,我有2个选项可以还原可用的用户对象:

However, I do have the same issues the other way around: when loading a user from a collection. AFAIK there is no way to change an objects prototype in JavaScript after it was created. And there's also no way to specify a prototype to use when Mongo instantiates objects it retrieved from a collection. So basically I always get objects that inherit from Object using Mongo. As far as I can tell I have 2 options to restore a usable user object from this point on:

  1. 创建一个继承自User的新鲜对象,并将结果对象上的每个属性复制到新创建的对象. (与存储机制1和3兼容)
  2. 创建一个继承自User的新鲜对象,并将结果对象作为数据属性存储在新创建的对象上. (与存储机制2兼容)
  1. Create a fresh object inheriting from User and copying each attribute on the result object to the newly created object. (Compatible to storing mechanisms 1 & 3)
  2. Create a fresh object inheriting from User and storing the result object as a data attribute on the newly created object. (Compatible to storing mechanism 2)

我的假设,特别是关于为查询结果指定原型的可能性是否正确?什么是正确的方法,为什么?我肯定不是第一个使用node.js在MongoDB中存储对象或从中复活对象的人.

Are my assumptions, especially about the possibility to specify a prototype for query results, correct? What's the right way to do it, and why? I'm surely not the first person struggling to store and resurrect objects in/from MongoDB using node.js.

目前,我将采用2/2的方法.我不太喜欢它,但是它是最有效的,也是唯一可以与API完美结合的工具.但是,我宁愿听到实际上API并没有做错任何事情,但我这样做是因为不知道如何正确使用它.所以,请赐教:)

Currently I would go with the approach 2/2. I don't really like it, but it is the most efficient and the only one that works cleanly with the API. However, I'd much rather hear that actually the API does nothing wrong, but I do for not knowing how to use it correctly. So please, enlighten me :)

推荐答案

我最近才意识到,实际上可以在V8/node中更改对象原型.尽管这不是标准,但它可以在各种浏览器中使用,尤其是在V8/node中!

I just recently realized, that it actually is possible to change an objects prototype in V8/node. While this is not in the standard it is possible in various browsers and especially in V8/node!

function User(username, email) {
    this.username = username;
    this.email = email;
}

User.prototype.sendMail = function (subject, text) {
    mailer.send(this.email, subject, text);
};

var o = {username: 'LoadeFromMongoDB', email: 'nomail@nomail.no'};
o.__proto__ = User.prototype;
o.sendMail('Hello, MongoDB User!', 'You where loaded from MongoDB, but inherit from User nevertheless! Congratulations!');

尽管不是ECMAScript标准,但该模块可用于各种模块和插件-即使是核心模块也可使用此技术.因此,我认为在node.js中使用是安全的.

This is used all over various modules and plugins - even core modules make use of this technique, allthough it is not ECMAScript standard. So I guess it is safe to use within node.js.

这篇关于在MongoDB中存储和检索JavaScript对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆