question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Model.save() doesn't save embedded arrays

See original GitHub issue

I have nested arrays of documents, and when I change them and do a .save() on the model it does not save the changes in the nested document.

Here are my models and schemas (some ommited for brevity):

var Votes = new mongoose.Schema({
    date: Date, 
    user_name: String,
    is_up: Boolean
});

var EventSchema = new mongoose.Schema({
  to_date: Date,
  from_date: Date,
  location: String,
  name: String,
  suggested_by: String,
  description: String,
  users: [EventUser],
  comments: [Comments],
  suggestions: [Event],
  votes: [Votes]
});

var Event = mongoose.model('Event', EventSchema);

Model before event.save() called:

{ 
     __v: 1,
    _id: 509e87e583ccbfa00e000004,
    description: 'Pfft',
    from_date: Sun Nov 11 2012 08:00:00 GMT-0500 (EST),
    location: 'Home',
    name: 'Whatever',
    suggested_by: 'No one',
    to_date: Sun Nov 11 2012 00:00:00 GMT-0500 (EST),
    votes: [],
    suggestions: 
     [ { users: [],
         comments: [],
         suggestions: [],
         votes: [],
         _id: 509e880883ccbfa00e000005,
         suggested_by: 'Some one',
         to_date: Sun Nov 11 2012 04:00:00 GMT-0500 (EST),
         from_date: Mon Nov 12 2012 00:00:00 GMT-0500 (EST),
         location: 'Home',
         name: 'Football',
         description: 'FOOTBALL!!' } ],
    comments: [],
    users: [] 
}

The same object with the nested votes right before event.save() is called.

{
   "__v":1,
   "_id":"509e87e583ccbfa00e000004",
   "description":"Pfft",
   "from_date":"2012-11-11T13:00:00.000Z",
   "location":"Home",
   "name":"Whatever",
   "suggested_by":"No one",
   "to_date":"2012-11-11T05:00:00.000Z",
   "votes":[ ],
   "suggestions":
      [ {
         "users":[],
         "comments":[ ],
         "suggestions":[ ],
         "votes":
            [{
               "is_up":true,
               "date":"2012-11-10T18:05:25.796Z",
               "user_name":"No one"
            }],
         "_id":"509e880883ccbfa00e000005",
         "suggested_by":"Some one",
         "to_date":"2012-11-11T09:00:00.000Z",
         "from_date":"2012-11-12T05:00:00.000Z",
         "location":"Home",
         "name":"Football",
         "description":"FOOTBALL!!"
      }],
   "comments":[],
   "users":[]
}

When event.save() is called, no error is thrown, but the nested votes schema inside of the nested events schema is not actually saved. If I use the same overall logic in the top level event object to save a vote, it does work.

When I looked through the code, briefly, it appears that .save() is suppose to be a shortcut for both saving new objects, as well as updating ones that already exist.

My hunch is Model.prototype._delta isn’t going deep enough to catch all nested objects, https://github.com/LearnBoost/mongoose/blob/master/lib/model.js#L529

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Comments:26

github_iconTop GitHub Comments

38reactions
4ladorcommented, Jan 27, 2016

I see what the issue is - classic case of the first question on the mongoose FAQ.

Thanks, it was this issue in my case ! 😃

model.myArray[index] = anyValue

becomes

model.myArray.set(index, anyValue)

32reactions
vkarpov15commented, Jun 20, 2015

I see what the issue is - classic case of the first question on the mongoose FAQ. Mongoose can’t track changes when you set an array index directly without something like ES6 proxies or ES7 Object.observe(). Use

chart.sizes[0].lengths.set(0, "20");

or

chart.sizes[0].lengths[0] = '20';
chart.markModified('sizes.0.lengths.0');
Read more comments on GitHub >

github_iconTop Results From Across the Web

Mongoose instance .save() not working when embedded ...
The problem is that mongoose don't knwo your array is modified. You can use 2 solutions : markModified. This function will mark the...
Read more >
Mongoose v6.8.1: SubDocuments
Subdocuments are documents embedded in other documents. ... Calling save() on the parent document triggers the save() middleware for all its subdocuments, ...
Read more >
[Solved]-Mongoose doesn't save nested Object in another ...
Coding example for the question Mongoose doesn't save nested Object in another nested Object-mongodb.
Read more >
Saving Your Data - 2.x - CakePHP Cookbook
CakePHP makes saving model data a snap. Data ready to be saved should be passed to the model's save() method using the following...
Read more >
db.collection.save() — MongoDB Manual
Starting in MongoDB 4.2, the db.collection.save() method is deprecated. ... the denormalized data model (embedded documents and arrays) will continue to be ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found