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

Support square bracket notation for Document.prototype.set(): set('arr[0].path', val) #11552

Open
gilles-yvetot opened this issue Mar 21, 2022 · 2 comments
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature

Comments

@gilles-yvetot
Copy link

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

What is the current behavior?
Cannot set a specific path

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

const mongoose = require('mongoose')
mongoose.set('debug', true)
const { Schema, model } = mongoose
const { Mixed } = Schema.Types

const VariableSchema = new Schema({
  type: String,
  valueType: String,
  value: Mixed,
  displayName: String,
  modifier: String,
})

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

const Step = model(`Step`, StepSchema)

const LoopConfigurationSchema = new Schema({
  loopItemsQueryPaths: [[VariableSchema]],
})
const LoopSchema = new Schema({
  configurations: {
    type: [LoopConfigurationSchema],
  },
})

Step.discriminator(`loop`, LoopSchema)

mongoose
  .connect('mongodb://localhost:27017/test', {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  })
  .then(async () => {
    let doc = await Step.create({
      type: `loop`,
      configurations: [
        {
          loopItemsQueryPaths: [],
        },
      ],
    })

    doc.set({
      'configurations[0].loopItemsQueryPaths': [
        [
          {
            type: 'object',
            valueType: 'qbManualEntry',
            value: '//div',
          },
        ],
      ],
    })
    await doc.save()
    console.dir(doc.configurations[0].loopItemsQueryPaths) // print [] 
    process.exit()
  })
  .catch((err) => {
    console.error(err)
    process.exit(1)
  })

What is the expected behavior?

doc.configurations[0].loopItemsQueryPaths should be set, right now it is []
The gist is pretty big but I tried to make it as small as I could. I have re-read everything to make sure there was no typo too.
It looks similar to another issue I opened recently

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

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Mar 21, 2022
@vkarpov15 vkarpov15 added this to the 6.2.12 milestone Mar 28, 2022
@vkarpov15
Copy link
Collaborator

I think the issue here is that we don't support square bracket syntax for set(). The below works:

    doc.set({
      'configurations.0.loopItemsQueryPaths': [
        [
          {
            type: 'object',
            valueType: 'qbManualEntry',
            value: '//div',
          },
        ],
      ],
    })

We'll take a look and see if this is something we can support

@vkarpov15
Copy link
Collaborator

We did some work to support this, including exporting stringToParts() from mpath, but it looks like this will be a more complex task than we thought because we need to support getting the schema path type of configurations[0].loopItemsQueryPaths in a couple of different cases. We'll postpone this to a future minor release.

@vkarpov15 vkarpov15 modified the milestones: 6.3.1, 6.x Unprioritized Apr 17, 2022
@vkarpov15 vkarpov15 added enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature and removed confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. labels Apr 17, 2022
@vkarpov15 vkarpov15 changed the title [BUG] Cannot update nested keys inside discriminator Support square bracket notation for Document.prototype.set(): set('arr[0].path', val) Apr 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Projects
None yet
Development

No branches or pull requests

3 participants