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

Pre 'findOneAndUpdate' hooks not working for array of child schemas #12694

Closed
2 tasks done
jcnmsg opened this issue Nov 15, 2022 · 2 comments · Fixed by #12735
Closed
2 tasks done

Pre 'findOneAndUpdate' hooks not working for array of child schemas #12694

jcnmsg opened this issue Nov 15, 2022 · 2 comments · Fixed by #12735
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Milestone

Comments

@jcnmsg
Copy link

jcnmsg commented Nov 15, 2022

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.7.2

Node.js version

14.16.1

MongoDB server version

4.11.0

Typescript version (if applicable)

No response

Description

Having a childSchema and using it inside an array on the mainSchema and then calling findOneAndUpdate on the model fires the findOneAndUpdate pre-hook on the mainSchema but not on the childSchema.

Steps to Reproduce

const childSchema = new mongoose.Schema({
   name: String
});

const mainSchema = new mongoose.Schema({
   child: [childSchema]
});

mainSchema.pre('findOneAndUpdate', function () {
    console.log('fired.2') // Works
});

childSchema.pre('findOneAndUpdate', function () {
    console.log('fired.1') // Doesn't work
});

const Model = mongoose.model('Model', mainSchema);
const test = new Model();

test.findOneAndUpdate({ _id: id }, {...data}, {}, (err, res) => {})

Expected Behavior

I would expect the console to log fired.1 and fired.2 but the pre-hook is only firing on the mainSchema. Also tried save, update and updateOne pre-hooks on childSchema without luck. Is this intended behavior or a bug?

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

IslandRhythms commented Nov 16, 2022

const mongoose = require('mongoose');

const childSchema = new mongoose.Schema({
  name: String
});

const mainSchema = new mongoose.Schema({
  child: [childSchema]
});

mainSchema.pre('findOneAndUpdate', function () {
   console.log('fired.2') // Works
});

childSchema.pre('findOneAndUpdate', function () {
   console.log('fired.1') // Doesn't work
});

const Test = mongoose.model('Test', mainSchema);

async function run() {
  await mongoose.connect('mongodb://localhost:27017');
  await mongoose.connection.dropDatabase();

  const entry = await Test.create({
    child: [{ name: 'Test' }]
  });

  await Test.findOneAndUpdate({_id: entry._id}, {child: [{name: 'Testerson'}, {name: 'Testserson'}]})

run();

@vkarpov15
Copy link
Collaborator

This is intentional behavior. We do not fire query middleware on subdocuments. We will add a note about this to the docs.

@vkarpov15 vkarpov15 added docs This issue is due to a mistake or omission in the mongoosejs.com documentation and removed confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. labels Nov 26, 2022
@vkarpov15 vkarpov15 added this to the 6.7.5 milestone Nov 26, 2022
vkarpov15 added a commit that referenced this issue Nov 30, 2022
docs(middleware): added note about execution policy on subdocuments
@vkarpov15 vkarpov15 modified the milestones: 6.7.6, 6.7.5 Nov 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants