猫鼬不保存嵌套对象 [英] Mongoose not saving nested object

查看:93
本文介绍了猫鼬不保存嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对猫鼬为什么不保存我的对象感到困惑:

I'm puzzled as of why Mongoose isn't saving my object:

var objectToSave = new ModelToSave({
  _id : req.params.id, 
  Item : customObject.Item //doesn't save with customObject.getItem() neither
});

但是正在保存;如下所示或带有硬编码的值:

But is saving this; as is below or with hardcoded values:

var objectToSave = new ModelToSave({
  _id : req.params.id, 
  Item : {
    SubItem : {
      property1 : customObject.Item.SubItem.property1, //also saves with customObject.getItem().SubItem.getProperty1()
      property2 : customObject.Item.SubItem.property2
    }
  }
});

getters/setter是

The getters/setters are

MyClass.prototype.getItem = function(){ ... };

我的Item对象很大,我宁愿不必指定每个子属性...

My Item object is quite big, and I'd rather not have to specify every single sub properties...

当我使用console.log(customObject.Item)查看Item对象时,或者当我通过API将其作为JSON返回时,它都具有我期望的所有嵌套属性(SubItem,...).

When I view my Item object with console.log(customObject.Item) or when I return it through my API as JSON, it has all the nested properties (SubItem, ...) that I'm expecting.

项目定义为:

SubItem = require('SubItemClass.js');

function MyClass(){
  this.Item = {
    SubItem : new SubItem()
  }
}

并将SubItem定义为

And SubItem is defined as

function SubItem(){
  this.property1 = '';
  this.property2 = 0;
}

该模型似乎可以正常工作,因为如果我对数据进行硬编码或指定要保存到模型的每个属性,则可以将数据保存到模型中.

The model seems to work as expected, because If I hardcode data or if I specify every single properties to save to the model, I can save the data to the Model...

反正这是代码:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var subItemDefinition = {
  Property1 : {type:String},
  Property2 : {type:Number},    
};

var itemDefinition = {
  SubItem : subItemDefinition
};

var customDefinition = {
  Item : itemDefinition
};

var customSchema = new Schema(customDefinition); 
module.exports = mongoose.model('ModelToSave', customSchema);

感谢您的帮助

推荐答案

我遇到了这种令人沮丧的情况,对Mongoose网站上记录的解决方案感到有些惊讶.

I came across this frustrating situation and was a little surprised by the documented solution from Mongoose's website.

所以这意味着要保存嵌套的数组/对象属性(在您的情况下为Item),则需要明确地指定更改.markModified('Item')

so what this means is to save nested array/object properties (Item in your case), you need to be explicit in specifying the change .markModified('Item')

var objectToSave = new ModelToSave({
  _id : req.params.id, 
  Item : customObject
});
objectToSave.markModified('Item');
objectToSave.save();

由于它是一种无模式类型,因此您可以将其值更改为您喜欢的任何其他值,但是Mongoose失去了自动检测和保存这些更改的功能.要告诉"猫鼬混合类型的值已更改,请调用文档的.markModified(path)方法,将路径传递到刚更改的混合类型.

Since it is a schema-less type, you can change the value to anything else you like, but Mongoose loses the ability to auto detect and save those changes. To "tell" Mongoose that the value of a Mixed type has changed, call the .markModified(path) method of the document passing the path to the Mixed type you just changed.

- http://mongoosejs.com/docs/schematypes.html#mixed

这篇关于猫鼬不保存嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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