skip to Main Content

It is known that we can update a MongoDB document atomically using the findAndModify method.

But how can we do it with a PanacheMongoRepository?

The PanacheMongoRepository interface has methods such as find and update, but nothing like findAndModify.

Is repository.update(updateDocument).where(queryDocument) an equivalent option? Is there a better way to do it?

2

Answers


  1. Disclaimer: I am not familiar with PanacheMongoRepository source code and how they implement things.

    However all these DB wrappers usually work in the same way, they just add syntax or notations about the native driver. Because they can’t change the actual DB commands then it’s usually safe to assume the same qualities are preserves across the same operators.

    This means if a MongoDB update is atomic, most likely "PanacheMongo" update is also atomic. The code snippet you posted is the correct way to update and that’s how you would execute it in MongoDB, just FYI findAndModify is deprecated. At the very least be wary of them.

    Login or Signup to reply.
  2. There is no equivalent of findAndModify on MongoDB with Panache.
    repository.update(updateDocument).where(queryDocument) do an update by query which updates multiple documents at once, whereas findAndModify finds and updates a single document.

    You can access the underlying MongoDB Collection by calling myRepository.mongoCollection() and using the method on the collection. It’s less convenient, but keep in mind that we didn’t intend to reflect all the capabilities of the MongoDB driver inside MongoDB with Panache but provide a convenient layer on top of it to simplify its usage on the most common patterns.

    If you think an addition should be made to offer this method directly on MongoDB with Panache, please open an issue in the Quarkus repository so we can discuss it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search