Skip to content

Conversation

@vkarpov15
Copy link
Collaborator

Summary

calling doc.updateOne() with an update pipeline currently fails in 9.1.0

Examples

Copy link
Contributor

Copilot AI left a 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 _update to 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' });
Copy link

Copilot AI Dec 30, 2025

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.

Copilot uses AI. Check for mistakes.
if (this._update == null || utils.isEmptyObject(this._update)) {
this._update = [];
} else {
throw new MongooseError('Cannot mix array and object updates');
Copy link
Collaborator

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.

@vkarpov15 vkarpov15 merged commit 18ca142 into master Dec 30, 2025
56 checks passed
@hasezoey hasezoey deleted the vkarpov15/fix-doc-updateone branch December 31, 2025 11:45
vkarpov15 added a commit that referenced this pull request Jan 4, 2026
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

Successfully merging this pull request may close these issues.

3 participants