猫鼬:"_ doc"怎么了? [英] Mongoose: what's up with "_doc"?

查看:62
本文介绍了猫鼬:"_ doc"怎么了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

猫鼬似乎在内部做一些非常时髦的事情.

It seems Mongoose is doing something really funky internally.

1 var Foo = new mongoose.model('Foo', new mongoose.Schema({a: String, b: Number}));
2 var foo = new Foo({a: 'test; b: 42}); 
3 var obj = {c: 1};
4 foo.goo = obj;                  // simple object assignment. obj should be 
                                  //   passed by reference to foo.goo. recall goo
                                  //   is not defined in the Foo model schema

5 console.log(foo.goo === obj);   // comparison directly after the assignment
    // => false, doesn't behave like normal JS object

基本上,任何时候您尝试处理非Mongoose模型的属性

Essentially, any time you try to deal with properties of a Mongoose model that aren't

a)在模型的架构中定义,或

a) defined in the model's schema or

b)定义为相同的类型(数组,obj,..)...该模型的行为甚至不像普通的Javascript对象.

b) defined as the same type (array, obj, ..) ... the model doesn't even behave like a normal Javascript object.

将行 4 切换到foo._doc.goo = obj将使控制台输出true.

Switching line 4 to foo._doc.goo = obj makes the console output true.

编辑:尝试重制怪异

示例1 :

 // Customer has a property 'name', but no property 'text'
 // I do this because I need to transform my data slightly before sending it
 // to client.
 models.Customer.find({}, function(err, data) {
     for (var i=0, len=data.length; i<len; ++i) {
        data[i] = data[i]._doc;            // if I don't do this, returned data
                                           // has no 'text' property
        data[i].text = data[i].name;       
    }
    res.json({success: err, response:data});
});

推荐答案

更新

也许我误解了您的原始问题,但现在看来您问题的性质发生了变化,因此以下信息无关紧要,但我将其保留. :)

我测试了您的代码,对我来说很好.当您设置不属于架构的属性(或其他一些特殊属性)时,Mongoose不会执行任何特殊代码. JavaScript当前不支持为尚不存在的属性调用代码(例如,Mongoose不会妨碍goo属性集).

I tested your code and it works fine for me. Mongoose doesn't execute any special code when you set properties that aren't part of the schema (or a few other special properties). JavaScript currently doesn't support calling code for properties that don't yet exist (so Mongoose can't get in the way of the set of the goo property for example).

因此,当您设置属性时:

So, when you set the property:

foo.goo = { c: 1 };

不涉及猫鼬.如果您的console.log不是您显示的代码,则可能会报告错误.

Mongoose isn't involved. If your console.log was something other than the code you displayed, I could see that it might report incorrectly.

此外,当您将结果send返回为JSON时,将调用JSON.stringify,这将在您的Mongoose模型上调用toString.发生这种情况时,Mongoose仅使用架构上定义的属性.因此,默认情况下不会发送任何其他属性.您已经更改了data数组的性质,但可以直接指向Mongoose数据,因此可以避免该问题.

Additionally, when you send the results back as JSON, JSON.stringify is being called, which calls toString on your Mongoose Model. When that happens, Mongoose only uses the properties defined on the schema. So, no additional properties are being sent back by default. You've changed the nature of the data array though to directly point at the Mongoose data, so it avoids that problem.

使用猫鼬设置属性goo时,会发生很多事情.猫鼬通过Object.defineProperty(一些

When you set the property goo using Mongoose, quite a few things happen. Mongoose creates property getters/setters via the Object.defineProperty (some docs). So, when you set the goo property, which you've defined as a [String], a few things happen:

  1. 在将值设置到对象实例之前会调用猫鼬代码(不同于简单的JavaScript对象)
  2. 猫鼬创建一个数组(可选)以存储将包含数组数据的数据(MongooseArray).在您提供的示例中,由于未传递数组,因此将创建该数组.
  3. 猫鼬会尝试将您的数据转换为正确的类型
  4. 它将对作为强制转换一部分传递的数据调用toString.
  1. Mongoose code is called prior to the value being set onto the object instance (unlike a simple JavaScript object)
  2. Mongoose creates an array (optionally) to store the data (a MongooseArray) which will contain the array data. In the example you provided, since you didn't pass an array, it will be created.
  3. Mongoose will attempt to cast your data to the right type
  4. It will call toString on the data passed as part of the cast.

因此,结果是该文档现在包含一个数组,该数组包含您传递的对象的toString版本.

So, the results are that the document now contains an array with a toString version of the object you passed.

如果检查了goo属性的内容,您会发现它现在是一个具有单个元素的数组,该元素是包含[object Object]的字符串.如果您选择了更基本的类型或与目标属性存储类型匹配,则会看到基本的相等性检查会起作用.

If you checked the contents of the goo property, you'd see that it's now an array with a single element, which is a string that contains [object Object]. If you'd picked a more basic type or matched the destination property storage type, you would see that a basic equality check would have worked.

这篇关于猫鼬:"_ doc"怎么了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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