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

Subdocument Hooks not triggered in Discriminators #15092

Closed
2 tasks done
kticka opened this issue Dec 13, 2024 · 3 comments
Closed
2 tasks done

Subdocument Hooks not triggered in Discriminators #15092

kticka opened this issue Dec 13, 2024 · 3 comments
Milestone

Comments

@kticka
Copy link

kticka commented Dec 13, 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

8.8.4

Node.js version

22.12.0

MongoDB server version

8.0.3

Typescript version (if applicable)

No response

Description

Hi, it seems, that hooks of embedded documents aren't triggered properly when used with discriminators. I wrote very simple example to reproduce.

Steps to Reproduce

Document.js

const mongoose    = require('mongoose')
const Subdocument = require('./Subdocument')

const schema = mongoose.Schema({
  name:         String,
  subdocuments: [Subdocument]
}, {discriminatorKey: 'type'})

schema.pre('save', function (next) {
  console.log(`Saving Document: ${this.name}`)
  next()
})

module.exports = mongoose.model('Document', schema)

Subdocument.js:

const mongoose = require('mongoose')

const schema = mongoose.Schema({
  name: String
})

schema.pre('save', function(next) {
  console.log(`Saving Subdocument: ${this.name}`)
  next()
})

module.exports = schema

Discriminator.js:

const mongoose = require('mongoose')
const Document = require('./Document')

const schema = mongoose.Schema({})

schema.pre('save', function(next) {
  console.log(`Saving Discriminator: ${this.name}`)
  next()
})

module.exports = Document.discriminator('Discriminator', schema)

Test.js

module.exports      = async function () {
  const document     = new Document({name: 'Document'})
  const dicriminator = new Discriminator({name: 'Discriminator', type: 'Discriminator'})

  document.subdocuments.push({name: 'Document Subdocument'})
  dicriminator.subdocuments.push({name: 'Discriminator Subdocument'})

  await document.save()
  await dicriminator.save()

  return 'Done'
}

Expected Behavior

When saving either Document or Discriminator, the Subdocument hook should be triggered. The expected output is:

Saving Subdocument: Document Subdocument
Saving Document: Document
Saving Subdocument: Discriminator Subdocument
Saving Document: Discriminator
Saving Discriminator: Discriminator

However, when saving the Discriminator, the Subdocument hook is not triggered. The actual output is:

Saving Subdocument: Document Subdocument
Saving Document: Document
Saving Document: Discriminator
Saving Discriminator: Discriminator
@kticka
Copy link
Author

kticka commented Dec 13, 2024

After further testing, I found that the issue does not occur in Mongoose 8.8.2. The problem starts in version 8.8.3 and persists in 8.8.4.

@maximilianschmid
Copy link

The problem starts with version 8.8.3 and is still there in 8.8.4 and 8.9. In my case the supdocuments with discriminators don't get the modified date updated.

@vkarpov15
Copy link
Collaborator

Confirmed that this issue will be fixed by #15099.

@vkarpov15 vkarpov15 added this to the 8.9.1 milestone Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants