Mongoose toObject:{virtuals:true} [英] Mongoose toObject: { virtuals: true }

查看:224
本文介绍了Mongoose toObject:{virtuals:true}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习MongoDB / Node,我注意到在模式中我经常会看到类似这样的内容:

I'm trying to learn MongoDB/Node and I noticed that in a schema I often see something like this:

toObject: { virtuals: true }
toJSON: { virtuals: true }

这些是做什么的两行是什么意思?

What do these two lines mean?

推荐答案

这不是MongoDB,而是特定于猫鼬ODM。

This is not "MongoDB" but specific to the mongoose ODM.

Mongoose有一个的概念模式定义中的虚拟字段。这基本上允许这个(来自文档的明显收集):

Mongoose has a concept of "virtual" fields in the schema definition. This essentially allow this (blatant glean from documentation):

var personSchema = new Schema({
    name: {
        first: String,
        last: String
    }
});

var Person = mongoose.model( "Person", personSchema );

但是假设你只是想存储那些属性,但是你可以在代码中访问一些东西全名。这就是虚拟的用武之地:

But suppose you just want to "store" those properties but then have something you can access in code called "fullname". This is where "virtuals" come in:

personSchema.virtual("name.full").get(function () {
    return this.name.first + ' ' + this.name.last;
});

现在我们可以这样做:

var bad = new Person({
    name: { "first": "Walter", "last": "White" }
});

console.log("%s is insane", bad.name.full); // Walter White is insane

所以 name.full 实际上并不存在于数据中,它只是代码中的模式表示。但当然绑定到一个函数,该函数使用对象中存在的实际数据来创建一个方法,该方法返回一个值,该值组合了方法中每个代码的两个字段。

So the name.full does not actually exist in the data, it's just a schema representation in code. But of course "tied" to a function that uses the actual data present in the object to make a method that returns a value combining the two fields per the code in the method.

这基本上就是虚拟字段。它们实际上是在文档对象上定义的方法,它呈现的值不是存储或持久存储在数据库中。通常它们基于数据存储中的实际持久值。

This is basically what "virtual" fields are about. They are actually "methods" defined on the document "object" that present a value that is not "stored" or persisted in the database. Usually they are based on actual persisted values from the data storage.

但要真正清除直接问题。 Mongoose默认只根据存储字段序列化内部对象结构的内容。那么这两行真正意味着:

But to really clear up your direct question. Mongoose only 'serializes' the content of it's internal object structure based on the "stored" fields by default. So what those two lines "really" mean are:


  1. toObject():这会产生一个没有扩展对象的所有其他mongoose magic部分的对象数据的普通或原始表示。但是虚拟的目的是使这些方法成为返回对象的一部分。基本上只是普通对象,称为:

  1. toObject(): This produces a "plain" or "raw" representation of the object data without all the other "mongoose magic" parts of the extended object. But the purpose of "virtuals" is to make those methods part of the object returned. Basically just the plain object, called as:

 var model = Model.new({ "name": { "first": "Walter", "last": "White" });
 console.log( model.toObject() );


  • toJSON():您可以显式调用此方法正如上面所示,但它最常见的用法来自下面的JSON解析器,它被隐式调用。同样的原则适用于上述。 虚拟包括序列化输出中这些方法的结果,例如:

  • toJSON(): You can call this method explicitly and just as shown above, but it's most common usage is from a JSON parser like below where it is implicitly called. The same principles apply as above. The "virtuals" includes the result of those methods in the serialized output, such as:

     var model = Model.new({ "name": { "first": "Walter", "last": "White" });
     JSON.stringify( model, undefined, 2 );
    


  • 所以第二种情况是隐含的 在对象上调用 .toJSON()方法。配置正在做的是告诉该方法不仅包括对象中存在的数据或字段,还包括定义的虚拟方法以及它们给出的输出。对于 .toObject()

    So the second case there is an "implicit" call of the .toJSON() method on the object. What the configuration is doing is telling that method to not only include data or "fields" present in the object, but also the "virtual" methods defined and the output they give as well. Same for .toObject().

    这篇关于Mongoose toObject:{virtuals:true}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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