morphia icon indicating copy to clipboard operation
morphia copied to clipboard

Datastore#replace(T entity) should not throw exception when no documents are updated

Open matheuscirillo opened this issue 6 months ago • 1 comments

So I'm testing 3.0.0 SNAPSHOT and I know this is a WIP, but I would like to suggest an improvement.

First of all, It has been working great so far.

I don't actually understand why we would have to throw an error in a replace operation when no document is updated. I would rather be able to use something like a ReplaceResult, just like the UpdateResult so I can decide if I have to throw an exception or not. It kinda breaks if I have an idempotent operation consumed by an user, for example, and I can't look to the exception type either, because it is too generic (MappingException).

When no document is updated, this error is thrown:

dev.morphia.mapping.MappingException: **No documents were updated by the operation for ID**:  0M40M2AR005YJ
	at dev.morphia.MorphiaDatastore.replace(MorphiaDatastore.java:493)

Maybe wrap the operation result within a ReplaceResult is a solution to give more control to the caller.

matheuscirillo avatar Jun 23 '25 21:06 matheuscirillo

You can set it to upsert and it'll do what you're asking.

evanchooly avatar Jun 25 '25 02:06 evanchooly