-
-
Notifications
You must be signed in to change notification settings - Fork 4k
fix(document): avoid 'Cannot mix array and object updates' on doc.updateOne() with pipeline #15928
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This pull request fixes a bug where calling doc.updateOne() with an update pipeline (array format) throws a "Cannot mix array and object updates" error in version 9.1.0.
Key Changes:
- Modified
Query.prototype._mergeUpdate()to handle the transition from an uninitialized or empty object_updateto an array-based pipeline update - Added a test case to verify that
doc.updateOne()works correctly with update pipelines
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| lib/query.js | Fixed _mergeUpdate() to allow converting an empty/uninitialized _update object to an array when merging a pipeline update, preventing false "Cannot mix array and object updates" errors |
| test/document.test.js | Added test case verifying that doc.updateOne() accepts update pipelines with the updatePipeline option |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const schema = new Schema({ name: String, age: Number }); | ||
| const Person = db.model('Person', schema); | ||
|
|
||
| const doc = new Person({ name: 'test' }); |
Copilot
AI
Dec 30, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test should save the document to the database before calling updateOne to ensure the test reflects realistic usage. Without saving, the document doesn't have a database record to update. Consider adding await doc.save(); before line 15136.
| if (this._update == null || utils.isEmptyObject(this._update)) { | ||
| this._update = []; | ||
| } else { | ||
| throw new MongooseError('Cannot mix array and object updates'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not directly related to this PR, but would be nice to have some more information in this error message to help debug.
Maybe the filter/update objects if possible.
…hen merging normal update with pipeline Re: #15928
Summary
calling
doc.updateOne()with an update pipeline currently fails in 9.1.0Examples