Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose not saving nested object

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
    }
  }
});

The getters/setters are

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

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

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.

Item is defined as:

SubItem = require('SubItemClass.js');

function MyClass(){
  this.Item = {
    SubItem : new 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...

here's the code anyway:

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);

Thanks for your help

like image 771
user1144446 Avatar asked Jun 05 '14 07:06

user1144446


People also ask

What does save () do in Mongoose?

Mongoose | save() Function The save() function is used to save the document to the database. Using this function, new documents can be added to the database.

Does Mongoose save return a Promise?

While save() returns a promise, functions like Mongoose's find() return a Mongoose Query . Mongoose queries are thenables. In other words, queries have a then() function that behaves similarly to the Promise then() function. So you can use queries with promise chaining and async/await.

Does Mongoose save overwrite?

Mongoose save with an existing document will not override the same object reference. Bookmark this question.

What is $Push in Mongoose?

$push. The $push operator appends a specified value to an array. The $push operator has the form: { $push: { <field1>: <value1>, ... } } To specify a <field> in an embedded document or in an array, use dot notation.


1 Answers

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

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();

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

like image 65
James Broad Avatar answered Sep 27 '22 23:09

James Broad