You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We are using Quarkus Mongo w/ Panache. The reads and creates are simple enough. However the updates are not so clear how to use. We need to be able to make atomic updates to existing Mongo records. I say atomic as I mean if two threads are updating the same record we want both updates to occur separately and only effect the fields that were changed in that update. Same for the other thread's update. Order of updates does not matter. Null fields must not change DB record value, e.g. PATCH REST semantics.
Currently I have a hack solution where I read the DB to get the current DB copy and then we use reflection to update any field that is not null (and then call update to save the updated record. However this is of course not atomic and we could end up remove some threads updates to the DB record.
However I was not clear how to use the various Panache update() methods except this one: update(Entity entity)
But now I see I think I could do this to update just the one record:
mongoConfigurationRepository.update(new Document("$set", update)).where(new Document("_id", configuration.id));
where update is a new Document that contains a map of the fields to update.
However I also see I could to this:
mongoConfigurationRepository
.mongoCollection()
.findOneAndUpdate(new Document("_id", configuration.id),
new Document("$set", update),
new FindOneAndUpdateOptions()
.returnDocument(ReturnDocument.AFTER));
where update is a new Document that contains a map of the fields to update.
Are these exactly the same? Is one atomic and the other not? The first is apparently designed to update any number of matching records via the where clause. However in the second case it will only find one and update that one. So is the second atomic and the first not atomic?
Is there a better way to do this? Atomic Mongo DB updates with PATCH semantics, It must ignore any DB field not in the update document.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
We are using Quarkus Mongo w/ Panache. The reads and creates are simple enough. However the updates are not so clear how to use. We need to be able to make atomic updates to existing Mongo records. I say atomic as I mean if two threads are updating the same record we want both updates to occur separately and only effect the fields that were changed in that update. Same for the other thread's update. Order of updates does not matter. Null fields must not change DB record value, e.g. PATCH REST semantics.
Currently I have a hack solution where I read the DB to get the current DB copy and then we use reflection to update any field that is not null (and then call update to save the updated record. However this is of course not atomic and we could end up remove some threads updates to the DB record.
However I was not clear how to use the various Panache update() methods except this one: update(Entity entity)
But now I see I think I could do this to update just the one record:
mongoConfigurationRepository.update(new Document("$set", update)).where(new Document("_id", configuration.id));
where update is a new Document that contains a map of the fields to update.
However I also see I could to this:
mongoConfigurationRepository
.mongoCollection()
.findOneAndUpdate(new Document("_id", configuration.id),
new Document("$set", update),
new FindOneAndUpdateOptions()
.returnDocument(ReturnDocument.AFTER));
where update is a new Document that contains a map of the fields to update.
Are these exactly the same? Is one atomic and the other not? The first is apparently designed to update any number of matching records via the where clause. However in the second case it will only find one and update that one. So is the second atomic and the first not atomic?
Is there a better way to do this? Atomic Mongo DB updates with PATCH semantics, It must ignore any DB field not in the update document.
Beta Was this translation helpful? Give feedback.
All reactions