Automattic / Automattic/mongoose

Run subdocument query middleware

Open
#11,043 2 comments 0 reactions 0 assignees View on GitHub
new feature
Dominant language
JavaScript
Stars
27.5k
Forks
4k
Avg merge
2d 7h
Merged PRs (30d)
35

Description

**Do you want to request a *feature* or report a *bug*?**
bug or own stupidity

**What is the current behavior?**
findAndUpdateOne Hook fires on Document, but not on Subdoc / SubSub.

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

Note: Using NestJS, unsure if that changes anything.
```ts
//hero.schema.ts
export const HeroSchema = new Schema({
name: String,
attribute: AttributeSchema,
});

HeroSchema.pre('findOneAndUpdate', function (next) {
console.log('Updating Hero');
next()
});

export const HeroModel = model('Hero', HeroSchema);

//attribute.schema.ts
export const AttributeSchema = new Schema({
attributes: {
cou: SingleAttributeSchema,
sgc: SingleAttributeSchema,
}});

AttributeSchema.pre('findOneAndUpdate', function (next) {
console.log('Updating Attribute');
next();
});

export const AttributeModel = model(
'Attribute',
AttributeSchema,
);

//singleAttribute.schema.ts
export const SingleAttributeSchema = new Schema({
value: Number,
});

SingleAttributeSchema.pre('findOneAndUpdate', function (next) {
console.log('Updating SingleAttribute');
next()
});

export const SingleAttributeModel = model(
'SingleAttribute',
SingleAttributeSchema,
);

//hero.service.ts
flattenedData = {
name: 'ab',
'attribute.attributes.cou.value': 9,
'attribute.attributes.sgc.value': 12
}

findByIdAndUpdate(
id,
{$set: flattenedData},
)
.exec();
// --> 'Updating Hero'
// MISSING!!! --> 'Updating Attribute'
// MISSING!!! --> 'Updating SingleAttribute'
// MISSING!!! --> 'Updating SingleAttribute'
```

```json
//tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"skipLibCheck": true,
"strictNullChecks": false,
"noImplicitAny": false,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false
}
}
```

**What is the expected behavior?**
Sub-Doc and Sub-Sub Hook fire as well.

**What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.**
Node 16.13.0
Mongoose ^6.0.13
MongDB 4.4

Contributor guide

Open the contributing guide

Research direction

Start with the hero.schema.ts, attribute.schema.ts, singleAttribute.schema.ts, and hero.service.ts reproduction, then run the findByIdAndUpdate example. Trace Mongoose query middleware and nested subdocument handling to establish the current behavior. Done means the expected middleware behavior for the document, subdocument, and nested subdocuments is covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, node.js, typescript
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.