BREAKING CHANGE: require passing both filter and update to Query.prototype.updateOne(), make Document.prototype.init() fully sync#16274
Open
vkarpov15 wants to merge 8 commits into
Open
BREAKING CHANGE: require passing both filter and update to Query.prototype.updateOne(), make Document.prototype.init() fully sync#16274vkarpov15 wants to merge 8 commits into
vkarpov15 wants to merge 8 commits into
Conversation
…otype.updateOne(), make Document.prototype.init() fully sync Fix #16269
Contributor
There was a problem hiding this comment.
Pull request overview
This PR removes remaining callback-based internals by making document hydration (Document#init()) fully synchronous, and introduces a breaking API change that requires explicitly passing both filter and update/replacement to Query.prototype.updateOne(), updateMany(), and replaceOne() to match the TypeScript signatures and avoid legacy ambiguity.
Changes:
- Make
Document.prototype.init()synchronous-only (no callback) and update internal hydration helpers accordingly. - Refactor query hydration completion paths (
_completeOne,completeOne,_completeMany,_completeOneLean) to remove callback plumbing. - Enforce
Query.prototype.updateOne()/updateMany()/replaceOne()requiring at least 2 args and rejecting callbacks; update affected tests.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
lib/query.js |
Removes callback-based hydration completion, enforces new Query update* signatures, and adjusts lean completion logic. |
lib/queryHelpers.js |
Makes createModelAndInit() synchronous and return the hydrated model instance. |
lib/document.js |
Makes Document#init() synchronous-only and removes callback support. |
lib/cursor/queryCursor.js |
Adapts cursor hydration to new synchronous createModelAndInit() with try/catch error handling. |
test/query.test.js |
Updates pre-hook error test to use new Query#updateOne(filter, update) signature and exec(op). |
test/model.updateOne.test.js |
Updates tests to pass explicit filter/update for query update methods and adjusts related coverage. |
Comments suppressed due to low confidence (2)
lib/query.js:4300
- The JSDoc examples for
Query.prototype.updateMany()still show chaining with a single argument (treating it as the update). Since this PR makesfilter+updaterequired, these examples should be updated (for example, pass{}as the filter when using chaining syntax) to avoid documenting an invalid signature.
* @param {object} filter the filter to match documents to update
* @param {object|Array} update the update command. If array, this update will be treated as an update pipeline and not casted.
* @param {object} [options]
* @param {boolean} [options.cloneUpdate=true] if `false`, Mongoose will not clone the update before executing the query
* @param {boolean} [options.multipleCastError] by default, mongoose only returns the first error that occurred in casting the query. Turn on this option to aggregate all the cast errors.
* @param {boolean|'throw'} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
lib/query.js:4359
- The JSDoc examples for
Query.prototype.updateOne()still showfindOne(...).updateOne(update)andupdateOne(update)as supported syntaxes, but this PR makesfilter+updaterequired. Please update or remove those examples so the docs match the new runtime behavior.
* @param {object} filter
* @param {object|Array} update the update command. If array, this update will be treated as an update pipeline and not casted.
* @param {object} [options]
* @param {boolean} [options.cloneUpdate=true] if `false`, Mongoose will not clone the update before executing the query
* @param {boolean} [options.multipleCastError] by default, mongoose only returns the first error that occurred in casting the query. Turn on this option to aggregate all the cast errors.
* @param {boolean|'throw'} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
Comment on lines
+2699
to
+2703
| return model.populate(doc, pop).then(doc => _completeOneLean(model.schema, doc, null, res, options)); | ||
| } | ||
|
|
||
| completeOne(model, doc, res, options, projection, userProvidedFields, [], (err, doc) => { | ||
| if (err != null) { | ||
| return callback(err); | ||
| } | ||
| model.populate(doc, pop).then(res => { callback(null, res); }, err => { callback(err); }); | ||
| }); | ||
| doc = completeOne(model, doc, res, options, projection, userProvidedFields, []); | ||
| return model.populate(doc, pop); |
hasezoey
requested changes
May 9, 2026
Collaborator
hasezoey
left a comment
There was a problem hiding this comment.
Sounds like a good change, but the Copilot comments should likely be investigated, also slight style suggestion.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix #16269
Summary
Remove some more callback-based internals:
Document.prototype.init()has no reason to support callbacks it is fully sync. Likewise, logic that uses it, likecompleteOne(), doesn't necessarily need to be async unless there'spopulate()involved.Also, for consistency with our TypeScript types and Model.updateX();
Query.prototype.updateOne(),updateMany(), andreplaceOne()now require both afilterandupdate/replacementargument. To avoid any confusion with our legacy behavior ofQuery.prototype.updateOne(obj)treatingobjas aupdatenot afilter.Examples