Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

immutable with upsert in nested object #9537

Closed
josephstgh opened this issue Nov 11, 2020 · 1 comment
Closed

immutable with upsert in nested object #9537

josephstgh opened this issue Nov 11, 2020 · 1 comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@josephstgh
Copy link

josephstgh commented Nov 11, 2020

Hi,

I'm trying to use upsert and immutable feature and it doesn't seem to work in nested object. Is it supposed to work this way or am I doing it wrong?

const mongoose = require('mongoose');

mongoose.set('useFindAndModify', false);

const { Schema } = mongoose;

run().catch(err => console.log(err));

async function run() {
  await mongoose.connect('mongodb://localhost:27017/upsert_immutable', {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  });

  await mongoose.connection.dropDatabase();

  const aInfo = {
    a: String,
    b: String,
    c: String
  }

  const schema = new Schema({
    name: String,
    description: String,
    xInfo: {
        type: aInfo,
        immutable: true,
        required: true,
    },
    yInfo: {
      type: String,
      required: true,
    },
    zInfo: {
      type: String,
      immutable: true,
      required: true,
  },
  })

  const newDoc = {
      name: 'name',
      description: 'new desc',
      xInfo: { a: 'a', b: 'b', c: 'c' },
      yInfo: 'yInfo',
      zInfo: 'zInfo'
  }

  const Model = mongoose.model('Test', schema);
  await Model.findOneAndUpdate({ name: 'name' }, newDoc, { upsert: true });

  console.log('Done', await Model.findOne(), new Date());
}

The result is

// notice that xInfo is not created but zInfo is
{
  _id: 5fabaad551adf51882270695,
  name: 'name',
  __v: 0,
  description: 'new desc',
  yInfo: 'yInfo',
  zInfo: 'zInfo'
}

node: v12.18.4
mongoose: 5.10.10
mongodb: 4.2.0

Any idea? Thanks!

@vkarpov15 vkarpov15 added this to the 5.10.14 milestone Nov 12, 2020
@vkarpov15 vkarpov15 added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Nov 12, 2020
vkarpov15 added a commit that referenced this issue Nov 12, 2020
@vkarpov15
Copy link
Collaborator

This is a bug in Mongoose, thanks for reporting. Fix will be in v5.10.14.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

2 participants