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

查看:27
本文介绍了猫鼬 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 }

这两行是什么意思?

推荐答案

这不是MongoDB",而是特定于 mongoose ODM.

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

Mongoose 在架构定义中有 "virtual" 字段的概念.这基本上允许这样做(从文档中公然收集):

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():这会生成对象数据的普通"或原始"表示,而没有扩展对象的所有其他猫鼬魔法"部分.但是虚拟"的目的是使这些方法成为返回对象的一部分.基本上只是普通对象,称为:

  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 解析器中隐式调用.与上述相同的原则适用.virtuals"包括序列化输出中这些方法的结果,例如:

  • 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().

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

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