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

[BUG] cannot update nested keys inside discrimator #11428

Closed
gilles-yvetot opened this issue Feb 18, 2022 · 3 comments
Closed

[BUG] cannot update nested keys inside discrimator #11428

gilles-yvetot opened this issue Feb 18, 2022 · 3 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@gilles-yvetot
Copy link

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

What is the current behavior?
One of my keys is not updated while the other one is. It looks like Mongoose stripped away the prop while calling Mongo

If the current behavior is a bug, please provide the steps to reproduce.

const StepSchema = new Schema({ url: String }, { discriminatorKey: `type` })

    const SheetReadOptionsSchema = new Schema({
      location: {
        id: String,
        timeout: Number,
      },
    })

    const ClickSchema = new Schema(
      [StepSchema, { sheetOptions: SheetReadOptionsSchema }],
      { discriminatorKey: `type` },
    )

    const Step = model(`Step`, StepSchema)

    Step.discriminator(`click`, ClickSchema)

    let doc = await Step.create({
      type: `click`,
      url: `https://google.com`,
      sheetOptions: {
        location: {
          id: `currentCell`,
          timeout: 1000,
        },
      },
    })

    doc.set({
      'sheetOptions.location.id': `inColumn`,
      'sheetOptions.location.timeout': 2000,
    })
    await doc.save()

    const updatedDoc = await Step.findOne({ type: `click`, _id: doc._id })
    console.log(updatedDoc.sheetOptions.location.id) // `currentCell` instead of `inColumn`
    console.log(updatedDoc.sheetOptions.location.timeout) // 2000 correct

What is the expected behavior?
updatedDoc.sheetOptions.location.id should display inColumn

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

mongoose 6.2.1
node 16.14.0
mongo 5

@gilles-yvetot
Copy link
Author

another data point: if SheetReadOptionsSchema keys are flatten out, i.e location is removed, id and timeout are added to the root, it works fine. It looks like it is because it is nested

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Feb 18, 2022
@IslandRhythms
Copy link
Collaborator

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

const StepSchema = new Schema({ url: String }, { discriminatorKey: `type` })

    const SheetReadOptionsSchema = new Schema({
      location: {
        id: String,
        timeout: Number,
      },
    })

    const ClickSchema = new Schema(
      [StepSchema, { sheetOptions: SheetReadOptionsSchema }],
      { discriminatorKey: `type` },
    )

    const Step = model(`Step`, StepSchema)

    Step.discriminator(`click`, ClickSchema)

    async function run() {
        await mongoose.connect('mongodb://localhost:27017')
        await mongoose.connection.dropDatabase();
        let doc = await Step.create({
            type: `click`,
            url: `https://google.com`,
            sheetOptions: {
              location: {
                id: `currentCell`,
                timeout: 1000,
              },
            },
          })
      
          doc.set({
            'sheetOptions.location.id': `inColumn`,
            'sheetOptions.location.timeout': 2000,
          })
          console.log('the doc', doc)
          await doc.save()
      
          const updatedDoc = await Step.findOne({ type: `click`, _id: doc._id })
          console.log(updatedDoc.sheetOptions.location.id) // `currentCell` instead of `inColumn`
          console.log(updatedDoc.sheetOptions.location.timeout) // 2000 correct
    }

    run();

@vkarpov15 vkarpov15 added this to the 6.2.4 milestone Feb 24, 2022
@gilles-yvetot
Copy link
Author

thanks @vkarpov15 and @IslandRhythms

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

3 participants