Mongoose: `document.set` does not update all values of nested schema

Created on 30 Mar 2019  路  4Comments  路  Source: Automattic/mongoose

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

Not all values of an object which is set to a nested path are updated, if the nested value is defined as Schema.

What is the expected behavior?

All values should be updated.

Code to reproduce

const { expect } = require('chai');
const { Schema, model } = require('mongoose');

it('does not updated all nested values inside a nested schema', () => {
  const schema = new Schema({
    nested: new Schema(
      {
        foo: Number,
        bar: Boolean,
      },
      {
        _id: false,
      }
    ),
  });
  const Model = model('first', schema);

  const entry = new Model({ nested: { foo: 666, bar: false } });

  entry.set('nested', { foo: 42, bar: true });

  // FAILS! foo == {foo: 666, bar: true}
  expect(entry.toObject().nested).to.deep.equal({
    foo: 42,
    bar: true,
  });
});

it('updates all nested values without a nested schema', () => {
  const schema = new Schema({
    nested: {
      foo: Number,
      bar: Boolean,
    },
  });
  const Model = model('second', schema);

  const entry = new Model({ nested: { foo: 123, bar: false } });

  entry.set('nested', { foo: 666, bar: true });

  // successes
  expect(entry.toObject().nested).to.deep.equal({
    foo: 666,
    bar: true,
  });
});

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

Node.js: 10.15.1
Mongoose: 5.4.20
MongoDb: Not needed for that behavior

confirmed-bug

Most helpful comment

Still happening with 5.5

All 4 comments

I seems that the problem does not occur with version 5.4.19

same here #7660

Still happening with 5.5

This was fixed with the fix for #7660 in 5.5.1. Thanks for your patience :+1:

Was this page helpful?
0 / 5 - 0 ratings