Skip to content

fix: "MongoServerError: The _id cannot be changed" when updating users#1230

Merged
kriswest merged 1 commit intofinos:mainfrom
andypols:fix-mongo-update-user-error
Sep 26, 2025
Merged

fix: "MongoServerError: The _id cannot be changed" when updating users#1230
kriswest merged 1 commit intofinos:mainfrom
andypols:fix-mongo-update-user-error

Conversation

@andypols
Copy link
Copy Markdown
Contributor

Summary

Updating a user (e.g., adding a GitHub username) currently fails with a MongoDB server exception. The issue arises because the user object already includes an _id, and the update attempt tries to overwrite it:

MongoServerError: The _id field cannot be changed

Fix

  • Removes the _id property from the user object before updating.
  • Performs an update by _id if one exists.
  • If no _id is present, inserts a new user with a no-op filter ({ username: user.username }).

@andypols andypols added the bug Something isn't working label Sep 26, 2025
@github-actions github-actions Bot added the fix label Sep 26, 2025
@netlify
Copy link
Copy Markdown

netlify Bot commented Sep 26, 2025

Deploy Preview for endearing-brigadeiros-63f9d0 canceled.

Name Link
🔨 Latest commit 050e70f
🔍 Latest deploy log https://app.netlify.com/projects/endearing-brigadeiros-63f9d0/deploys/68d6c2e744bc470008aa04d8

@codecov
Copy link
Copy Markdown

codecov Bot commented Sep 26, 2025

Codecov Report

❌ Patch coverage is 25.00000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.01%. Comparing base (32bfc70) to head (050e70f).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
src/db/mongo/users.ts 25.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1230      +/-   ##
==========================================
- Coverage   84.06%   84.01%   -0.06%     
==========================================
  Files          68       68              
  Lines        2944     2947       +3     
  Branches      373      374       +1     
==========================================
+ Hits         2475     2476       +1     
- Misses        409      411       +2     
  Partials       60       60              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Contributor

@kriswest kriswest left a comment

Choose a reason for hiding this comment

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

LGTM

Are similar changes needed in any of the other dB clients?

@andypols
Copy link
Copy Markdown
Contributor Author

@kriswest I did look for other mongo updates, but they all look good.

@kriswest kriswest merged commit db1debc into finos:main Sep 26, 2025
14 checks passed
@andypols andypols deleted the fix-mongo-update-user-error branch September 26, 2025 17:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants