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

Disallow changing discriminator key in update unless overwriteDiscriminatorKey option set #9187

Closed
vkarpov15 opened this issue Jun 30, 2020 · 1 comment
Assignees
Milestone

Comments

@vkarpov15
Copy link
Collaborator

Re: #6087, now that we have an option to explicitly allow changing the discriminator key, we should disallow setting the discriminator key in updateOne(), etc. unless that option is set.

@vkarpov15 vkarpov15 added this to the 6.0 milestone Jun 30, 2020
@vkarpov15
Copy link
Collaborator Author

Here's a script that should succeed in 6.0:

const assert = require('assert');
const mongoose = require('mongoose');

mongoose.set('debug', true);
mongoose.set('useFindAndModify', false);

const { Schema } = mongoose;

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

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

  await mongoose.connection.dropDatabase();

  const baseSchema = new Schema({}, { discriminatorKey: 'type' });
  const baseModel = mongoose.model('thing', baseSchema);

  const aSchema = new Schema(
    {
      aThing: { type: Number },
    },
    { _id: false, id: false },
  );
  const aModel = baseModel.discriminator('A', aSchema);

  const bSchema = new Schema(
    {
      bThing: { type: String },
    },
    { _id: false, id: false },
  );
  const bModel = baseModel.discriminator('B', bSchema);

  // Model is created as a type A
  const doc = await baseModel.create({ type: 'A', aThing: 1 });

  const res = await baseModel.findByIdAndUpdate(
    doc._id,
    { type: 'A', bThing: 'one', aThing: '2' },
    { runValidators: true, /*overwriteDiscriminatorKey: true*/ },
  );
  assert.equal(res.type, 'B');
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants