Skip to content
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

SaveAsync fails to upsert when using sharded MongoDB #230

Open
MichaelGlaive opened this issue Oct 15, 2024 · 0 comments
Open

SaveAsync fails to upsert when using sharded MongoDB #230

MichaelGlaive opened this issue Oct 15, 2024 · 0 comments
Labels
enhancement new feature or request up for grabs feature requires community contribution

Comments

@MichaelGlaive
Copy link

MichaelGlaive commented Oct 15, 2024

When running MongoDB with sharding, using SaveAsync to upsert an entity causes an exception with the following text:

MongoDB.Driver.MongoWriteException: A write operation resulted in an error. WriteError: { Category : "Uncategorized", Code : 61, Message : "Failed to target upsert by query :: could not extract exact shard key" }.

The reason for it is that the sharding field have to appear in the u section of the upsert query, but SaveAsync doesn't account for it.
Only changing the code to use UpdateAsync and explicitly adding Match with the sharding field results in a working solution.
Sharding enabled on MongoDB breaks SaveAsync, and there's no way to overcome it in the existing version of the nuget, other than not using that method at all.
My guess would be that any update operation would fail where the fields that participate in sharding are not present.

I'm considering trying to change code to use DbContext with global filters, to see if that could help solve it, but that would be replacing 100s of DB.Something calls to use DbContext... Also it's not going to work with just myEntity.SaveAsync, because it uses the global DB static class as far as I understand.

P.S.: Thank you for your great work on this nuget, it's amazing for all the non-sharded scenarios that I've encountered so far.

UPDATE: Global filters are not going to work, according to the documentation:

only delete, update and retrieval operations will use global filters. the Save*() operations will ignore any registered global filters as they will only match on the ID field.

@dj-nitehawk dj-nitehawk added enhancement new feature or request up for grabs feature requires community contribution labels Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement new feature or request up for grabs feature requires community contribution
Projects
None yet
Development

No branches or pull requests

2 participants