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

Global strictQuery flag should apply to arrayFilters #11836

Closed
postama opened this issue May 26, 2022 · 0 comments
Closed

Global strictQuery flag should apply to arrayFilters #11836

postama opened this issue May 26, 2022 · 0 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@postama
Copy link

postama commented May 26, 2022

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

What is the current behavior?
An error is thrown when using arrayFilters that do not match the schema, even if strictQuery is set to false.
This is similar to #11062 which fixed it on the query-by-query basis, but not with the global flag.

If the current behavior is a bug, please provide the steps to reproduce.
Error: Could not find path "items.0.oldProp" in schema when strictQuery is set to false

const mongoose = require('mongoose')
mongoose.set('strictQuery', false)
const { Schema } = mongoose
const uri = `mongodb://localhost:27017/test`
console.log(`Mongoose version: ${mongoose.version}`)

const ItemSchema = new Schema({
  newProp: {
    type: String
  }
})

//Schema
const FooSchema = new Schema({
  foo: {
    type: String
  },
  items: [ItemSchema]
})

//Model
const Foo = mongoose.model('Foo', FooSchema)

//Test function
async function test() {
  //Connect to database
  await mongoose.connect(uri)
  await mongoose.connection.dropDatabase()

  //Create new item
  const foo = {
    items: [
      {
        oldProp: false
      },
      {
        oldProp: false
      }
    ]
  }
  await Foo.create(foo, { strict: false })

  await Foo.updateMany(
    {},
    {
      $set: { 'items.$[m].newProp': 'test' }
    },
    {
      arrayFilters: [
        {
          'm.oldProp': false
        }
      ]
    }
  )
}

test()

What is the expected behavior?
This update works if strict is set to false on the individual query, but it should respect the global strictQuery flag as well, and not throw an error.

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

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label May 26, 2022
@vkarpov15 vkarpov15 added this to the 6.3.7 milestone Jun 7, 2022
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