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

Add overwriteDiscriminatorKey to API docs for findOneAndUpdate(), updateOne(); and findOneAndUpdate() guide #14246

Closed
2 tasks done
akhilkcg opened this issue Jan 9, 2024 · 2 comments
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Milestone

Comments

@akhilkcg
Copy link

akhilkcg commented Jan 9, 2024

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.12.5

Node.js version

14.18.1

MongoDB server version

3.6.6

Typescript version (if applicable)

No response

Description

In 5.x findOneAndUpdate successfully updates the value of a discriminatorKey field. However, in 6.x it does not

Steps to Reproduce

Repro script:

const mongoose = require('mongoose')
const Schema = mongoose.Schema

const superheroSchema = new Schema({
  name: { type: String },
  universe: { type: String }
}, { discriminatorKey: 'powerType' })

const Superhero = mongoose.model('Superhero', superheroSchema)
Superhero.discriminator('Money', new Schema({ money: { type: Number } }), { clone: false })
Superhero.discriminator('Strength', new Schema({ strength: { type: Number } }), { clone: false })

async function run() {
  await mongoose.connect('mongodb://localhost:27017');

  const doc = new Superhero({ name: 'Batman', universe: 'DC', powerType: 'Money', money: 100 });
  await doc.save()

  const updatedDoc = await Superhero.findOneAndUpdate({_id: doc._id}, { powerType: 'Strength' }, { new: true })
  console.log('new powerType:', updatedDoc.powerType)

  await mongoose.disconnect();
}

run()

Expected Behavior

Output in 5.12.5

new powerType: Strength

Output in 6.12.5

new powerType: Money

6.x must behave like 5.x

@akhilkcg akhilkcg changed the title findOneAndUpdate behaves differently while updating a discrimatorKey in 5.x vs 6.x findOneAndUpdate behaves differently while updating value of a discrimatorKey field in 5.x vs 6.x Jan 9, 2024
@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Jan 11, 2024
@vkarpov15
Copy link
Collaborator

This is intentional behavior; starting in Mongoose 6 you need to set the overwriteDiscriminatorKey option to modify the discriminator key in an update as described here: https://mongoosejs.com/docs/6.x/docs/discriminators.html#updating-the-discriminator-key

@vkarpov15 vkarpov15 added docs This issue is due to a mistake or omission in the mongoosejs.com documentation and removed confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. labels Jan 11, 2024
@vkarpov15 vkarpov15 added this to the 7.6.9 milestone Jan 11, 2024
@vkarpov15 vkarpov15 changed the title findOneAndUpdate behaves differently while updating value of a discrimatorKey field in 5.x vs 6.x Add overwriteDiscriminatorKey to API docs for findOneAndUpdate(), updateOne(); and findOneAndUpdate() guide Jan 11, 2024
@akhilkcg
Copy link
Author

thanks @vkarpov15

@akhilkcg akhilkcg reopened this Jan 12, 2024
vkarpov15 added a commit that referenced this issue Jan 17, 2024
vkarpov15 added a commit that referenced this issue Jan 18, 2024
docs(model+query+findoneandupdate): add more details about `overwriteDiscriminatorKey` option to docs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Projects
None yet
Development

No branches or pull requests

3 participants