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

Nested properties of non-strict schema are undefined after reload #11309

Closed
adamreisnz opened this issue Feb 1, 2022 · 2 comments
Closed

Nested properties of non-strict schema are undefined after reload #11309

adamreisnz opened this issue Feb 1, 2022 · 2 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. priority Automatically set for Mongoose Pro subscribers
Milestone

Comments

@adamreisnz
Copy link
Contributor

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

What is the current behavior?
Nested properties of a non-strict schema appear as undefined after a document is loaded from the database

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

const mongoose = require('mongoose')
const {Schema} = mongoose
const uri = `mongodb://localhost:27017/test`

mongoose.set('debug', true)
console.log(`Mongoose version: ${mongoose.version}`)

//Schema
const NestedSchema = new Schema({}, {
  id: false,
  _id: false,
  strict: false,
})

const ItemSchema = new Schema({
  name: {
    type: String,
  },
  nested: NestedSchema,
})

//Model
const Item = mongoose.model('Item', ItemSchema)

//Run function
async function run() {
  await mongoose.connect(uri)

  //Create new item
  const item = await Item.create({
    name: 'Test',
    nested: {
      something: 1,
      else: 'test',
      foo: {
        bar: 55,
      },
    },
  })

  console.log(`After creation: ${item.nested.foo.bar}`)

  //Modify nested data
  item.nested.foo.bar = 66
  item.markModified('nested.foo.bar')
  await item.save()

  console.log(`After save: ${item.nested.foo.bar}`)

  //Reload
  const reloaded = await Item.findOne({_id: item._id})
  console.log(`After reload: ${reloaded.nested}`)
  console.log(`After reload: ${reloaded.nested.foo}`)
  console.log(`After reload: ${reloaded.nested.foo.bar}`)
}

//Run
run().catch(err => console.log(err))

What is the expected behavior?
To be able to log the nested property foo and see it's value after reloading the item from the database.
Instead, foo is undefined.

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

@mongoose-pro-bot mongoose-pro-bot added the priority Automatically set for Mongoose Pro subscribers label Feb 2, 2022
@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Feb 2, 2022
@vkarpov15 vkarpov15 added this to the 6.2.1 milestone Feb 6, 2022
@adamreisnz
Copy link
Contributor Author

Thanks @vkarpov15, so this was a confirmed issue?

@vkarpov15
Copy link
Collaborator

@adamreisnz yes this is a confirmed bug and will be fixed in v6.2.1

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. priority Automatically set for Mongoose Pro subscribers
Projects
None yet
Development

No branches or pull requests

4 participants