Skip to content

Conversation

@Jadu07
Copy link
Contributor

@Jadu07 Jadu07 commented Nov 10, 2025

This PR addresses feature request #14351 and introduces an enhancement for the insertMany() method when used with { ordered: false }
It adds two new properties to validation errors:

  • error.index: the index of the failed document in the input array

These additions align Mongoose’s error reporting more closely with MongoDB’s native write errors and make debugging bulk inserts easier.


Problem

Currently, when using insertMany() with { ordered: false, rawResult: true }, validation errors lack:

  1. The index of the failed document in the original array

Without this, it’s difficult to identify and retry failed documents in production.


Solution

Modified Model.$__insertMany() to add two properties to validation errors when ordered: false:

  • error.index: The position of the failing document in the original array

Before:

{
  errors: {
    requiredField: {
      name: 'ValidatorError',
      message: 'requiredField is required.',
      // ...
    }
  },
  _message: 'User validation failed',
  name: 'ValidationError',
  message: 'User validation failed: requiredField: requiredField is required.'
}

After:

{
  errors: {
    requiredField: {
      name: 'ValidatorError',
      message: 'requiredField is required.',
      // ...
    }
  },
  index: 2,  // NEW: Position in the array
  _message: 'User validation failed',
  name: 'ValidationError',
  message: 'User validation failed: requiredField: requiredField is required.'
}

Testing

  • All 3,943 existing tests pass
  • Added 2 new test cases that verify:
    • index property matches the array position
    • Works with both rawResult and throwOnValidationError options

fixes #14351

@Jadu07
Copy link
Contributor Author

Jadu07 commented Nov 11, 2025

Hey @vkarpov15 @hasezoey pls review this PR.

Copy link
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

@hasezoey hasezoey added enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature developer-experience This issue improves error messages, debugging, or reporting labels Nov 11, 2025
@Jadu07 Jadu07 requested a review from hasezoey November 11, 2025 17:09
Copy link
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still looks good to me

@hasezoey hasezoey requested a review from vkarpov15 November 11, 2025 17:38
@vkarpov15 vkarpov15 requested a review from Copilot November 13, 2025 21:17
Copilot finished reviewing on behalf of vkarpov15 November 13, 2025 21:19
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 PR enhances the insertMany() method to add an index property to validation errors when using { ordered: false }. The addition helps developers identify which document in the input array failed validation, aligning with MongoDB's native error reporting pattern.

Key changes:

  • Added index property to ValidationError type definition
  • Modified validation error handling to set the index property in unordered inserts
  • Added test coverage for the new index property in two scenarios

Reviewed Changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 1 comment.

File Description
types/error.d.ts Added optional index property to ValidationError type definition with JSDoc
lib/model.js Set error.index during validation error handling for unordered inserts
test/model.insertMany.test.js Added two test cases verifying index property with rawResult and throwOnValidationError options

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@vkarpov15 vkarpov15 added this to the 8.19.4 milestone Nov 13, 2025
Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 👍

@vkarpov15 vkarpov15 merged commit c8ccd90 into Automattic:master Nov 13, 2025
47 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

developer-experience This issue improves error messages, debugging, or reporting enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create mongoose validationError object similar to mongodb error in insertMany

3 participants