diff --git a/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/reactive/runtime/ReactiveMongoOperations.java b/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/reactive/runtime/ReactiveMongoOperations.java index bd4c3ae51c7b8..0e27d7536b8ad 100644 --- a/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/reactive/runtime/ReactiveMongoOperations.java +++ b/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/reactive/runtime/ReactiveMongoOperations.java @@ -42,6 +42,14 @@ public abstract class ReactiveMongoOperations { public final String ID = "_id"; private static final Logger LOGGER = Logger.getLogger(ReactiveMongoOperations.class); + + // update operators: https://docs.mongodb.com/manual/reference/operator/update/ + private static final List UPDATE_OPERATORS = Arrays.asList( + "$currentDate", "$inc", "$min", "$max", "$mul", "$rename", "$set", "$setOnInsert", "$unset", + "$addToSet", "$pop", "$pull", "$push", "$pullAll", + "$each", "$position", "$slice", "$sort", + "$bit"); + private static final Map defaultDatabaseName = new ConcurrentHashMap<>(); protected abstract QueryType createQuery(ReactiveMongoCollection collection, Document query, Document sortDoc); @@ -369,11 +377,11 @@ public String bindFilter(Class clazz, String query, Map param /** * We should have a query like {'firstname': ?1, 'lastname': ?2} for native one * and like firstname = ?1 and lastname = ?2 for PanacheQL one. - * As update document needs a $set operator we add it if needed. + * As update document needs an update operator, we add $set if none is provided. */ String bindUpdate(Class clazz, String query, Object[] params) { String bindUpdate = bindQuery(clazz, query, params); - if (!bindUpdate.contains("$set")) { + if (!containsUpdateOperator(query)) { bindUpdate = "{'$set':" + bindUpdate + "}"; } LOGGER.debug(bindUpdate); @@ -383,17 +391,26 @@ String bindUpdate(Class clazz, String query, Object[] params) { /** * We should have a query like {'firstname': :firstname, 'lastname': :lastname} for native one * and like firstname = :firstname and lastname = :lastname for PanacheQL one. - * As update document needs a $set operator we add it if needed. + * As update document needs an update operator, we add $set if none is provided. */ String bindUpdate(Class clazz, String query, Map params) { String bindUpdate = bindQuery(clazz, query, params); - if (!bindUpdate.contains("$set")) { + if (!containsUpdateOperator(query)) { bindUpdate = "{'$set':" + bindUpdate + "}"; } LOGGER.debug(bindUpdate); return bindUpdate; } + private boolean containsUpdateOperator(String update) { + for (String operator : UPDATE_OPERATORS) { + if (update.contains(operator)) { + return true; + } + } + return false; + } + String bindQuery(Class clazz, String query, Object[] params) { String bindQuery = null; diff --git a/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/runtime/MongoOperations.java b/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/runtime/MongoOperations.java index e40b104f6b709..9ed4225e27517 100644 --- a/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/runtime/MongoOperations.java +++ b/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/runtime/MongoOperations.java @@ -42,6 +42,13 @@ public abstract class MongoOperations { public final String ID = "_id"; private static final Logger LOGGER = Logger.getLogger(MongoOperations.class); + // update operators: https://docs.mongodb.com/manual/reference/operator/update/ + private static final List UPDATE_OPERATORS = Arrays.asList( + "$currentDate", "$inc", "$min", "$max", "$mul", "$rename", "$set", "$setOnInsert", "$unset", + "$addToSet", "$pop", "$pull", "$push", "$pullAll", + "$each", "$position", "$slice", "$sort", + "$bit"); + private final Map defaultDatabaseName = new ConcurrentHashMap<>(); protected abstract QueryType createQuery(MongoCollection collection, Document query, Document sortDoc); @@ -343,11 +350,11 @@ public String bindFilter(Class clazz, String query, Map param /** * We should have a query like {'firstname': ?1, 'lastname': ?2} for native one * and like firstname = ?1 and lastname = ?2 for PanacheQL one. - * As update document needs a $set operator we add it if needed. + * As update document needs an update operator, we add $set if none is provided. */ String bindUpdate(Class clazz, String query, Object[] params) { String bindUpdate = bindQuery(clazz, query, params); - if (!bindUpdate.contains("$set")) { + if (!containsUpdateOperator(query)) { bindUpdate = "{'$set':" + bindUpdate + "}"; } LOGGER.debug(bindUpdate); @@ -357,17 +364,26 @@ String bindUpdate(Class clazz, String query, Object[] params) { /** * We should have a query like {'firstname': :firstname, 'lastname': :lastname} for native one * and like firstname = :firstname and lastname = :lastname for PanacheQL one. - * As update document needs a $set operator we add it if needed. + * As update document needs an update operator, we add $set if none is provided. */ String bindUpdate(Class clazz, String query, Map params) { String bindUpdate = bindQuery(clazz, query, params); - if (!bindUpdate.contains("$set")) { + if (!containsUpdateOperator(query)) { bindUpdate = "{'$set':" + bindUpdate + "}"; } LOGGER.debug(bindUpdate); return bindUpdate; } + private boolean containsUpdateOperator(String update) { + for (String operator : UPDATE_OPERATORS) { + if (update.contains(operator)) { + return true; + } + } + return false; + } + private String bindQuery(Class clazz, String query, Object[] params) { String bindQuery = null; //determine the type of the query diff --git a/extensions/panache/mongodb-panache-kotlin/runtime/src/main/kotlin/io/quarkus/mongodb/panache/kotlin/PanacheMongoCompanion.kt b/extensions/panache/mongodb-panache-kotlin/runtime/src/main/kotlin/io/quarkus/mongodb/panache/kotlin/PanacheMongoCompanion.kt index e2bd0db004a1d..ea00195eaebc2 100644 --- a/extensions/panache/mongodb-panache-kotlin/runtime/src/main/kotlin/io/quarkus/mongodb/panache/kotlin/PanacheMongoCompanion.kt +++ b/extensions/panache/mongodb-panache-kotlin/runtime/src/main/kotlin/io/quarkus/mongodb/panache/kotlin/PanacheMongoCompanion.kt @@ -647,7 +647,8 @@ interface PanacheMongoCompanionBase { * Update all entities of this type using the given update document with optional indexed parameters. * The returned [PanacheUpdate] object will allow to restrict on which document the update should be applied. * - * @param update the update document, if it didn't contain `$set` we add it. It can also be expressed as a query string. + * @param update the update document, if it didn't contain any update operator, we add `$set`. + * It can also be expressed as a query string. * @param params optional sequence of indexed parameters * @return a new [PanacheUpdate] instance for the given update document * @see [update] @@ -659,7 +660,7 @@ interface PanacheMongoCompanionBase { * Update all entities of this type by the given update document with named parameters. * The returned [PanacheUpdate] object will allow to restrict on which document the update should be applied. * - * @param update the update document, if it didn't contain `$set` we add it. + * @param update the update document, if it didn't contain any update operator, we add `$set`. * It can also be expressed as a query string. * * @param params map of named parameters @@ -674,8 +675,8 @@ interface PanacheMongoCompanionBase { * Update all entities of this type by the given update document, with named parameters. * The returned [PanacheUpdate] object will allow to restrict on which document the update should be applied. * - * @param update the update document, if it didn't contain `$set` we add it. It can also be expressed as a query - * string. + * @param update the update document, if it didn't contain any update operator, we add `$set`. + * It can also be expressed as a query string. * * @param params [Parameters] of named parameters * @return a new [PanacheUpdate] instance for the given update document diff --git a/extensions/panache/mongodb-panache-kotlin/runtime/src/main/kotlin/io/quarkus/mongodb/panache/kotlin/PanacheMongoRepositoryBase.kt b/extensions/panache/mongodb-panache-kotlin/runtime/src/main/kotlin/io/quarkus/mongodb/panache/kotlin/PanacheMongoRepositoryBase.kt index aa5d2f901af1e..40aeb0c9e4a7a 100755 --- a/extensions/panache/mongodb-panache-kotlin/runtime/src/main/kotlin/io/quarkus/mongodb/panache/kotlin/PanacheMongoRepositoryBase.kt +++ b/extensions/panache/mongodb-panache-kotlin/runtime/src/main/kotlin/io/quarkus/mongodb/panache/kotlin/PanacheMongoRepositoryBase.kt @@ -678,7 +678,7 @@ interface PanacheMongoRepositoryBase { * Update all entities of this type by the given update document, with optional indexed parameters. * The returned [PanacheUpdate] object will allow to restrict on which documents the update should be applied. * - * @param update the update document, if it didn't contain `$set` we add it. + * @param update the update document, if it didn't contain any update operator, we add `$set`. * It can also be expressed as a query string. * @param params optional sequence of indexed parameters * @return a new [PanacheUpdate] instance for the given update document @@ -691,7 +691,7 @@ interface PanacheMongoRepositoryBase { * Update all entities of this type by the given update document, with named parameters. * The returned [PanacheUpdate] object will allow to restrict on which documents the update should be applied. * - * @param update the update document, if it didn't contain `$set` we add it. + * @param update the update document, if it didn't contain any update operator, we add `$set`. * It can also be expressed as a query string. * @param params [Map] of named parameters * @return a new [PanacheUpdate] instance for the given update document @@ -705,7 +705,7 @@ interface PanacheMongoRepositoryBase { * Update all entities of this type by the given update document, with named parameters. * The returned [PanacheUpdate] object will allow to restrict on which document the update should be applied. * - * @param update the update document, if it didn't contain `$set` we add it. + * @param update the update document, if it didn't contain any update operator, we add `$set`. * It can also be expressed as a query string. * @param params [Parameters] of named parameters * @return a new [PanacheUpdate] instance for the given update document diff --git a/extensions/panache/mongodb-panache-kotlin/runtime/src/main/kotlin/io/quarkus/mongodb/panache/kotlin/reactive/ReactivePanacheMongoCompanion.kt b/extensions/panache/mongodb-panache-kotlin/runtime/src/main/kotlin/io/quarkus/mongodb/panache/kotlin/reactive/ReactivePanacheMongoCompanion.kt index 39db5694be8e6..ee54a438afa79 100644 --- a/extensions/panache/mongodb-panache-kotlin/runtime/src/main/kotlin/io/quarkus/mongodb/panache/kotlin/reactive/ReactivePanacheMongoCompanion.kt +++ b/extensions/panache/mongodb-panache-kotlin/runtime/src/main/kotlin/io/quarkus/mongodb/panache/kotlin/reactive/ReactivePanacheMongoCompanion.kt @@ -720,7 +720,7 @@ interface ReactivePanacheMongoCompanionBase { * Update all entities of this type by the given update document, with optional indexed parameters. * The returned [ReactivePanacheUpdate] object will allow to restrict on which document the update should be applied. * - * @param update the update document, if it didn't contain `$set` we add it. + * @param update the update document, if it didn't contain any update operator, we add `$set`. * It can also be expressed as a query string. * @param params optional sequence of indexed parameters * @return a new [ReactivePanacheUpdate] instance for the given update document @@ -699,7 +699,7 @@ interface ReactivePanacheMongoRepositoryBase { * Update all entities of this type by the given update document, with named parameters. * The returned [ReactivePanacheUpdate] object will allow to restrict on which document the update should be applied. * - * @param update the update document, if it didn't contain `$set` we add it. + * @param update the update document, if it didn't contain any update operator, we add `$set`. * It can also be expressed as a query string. * @param params [Map] of named parameters * @return a new [ReactivePanacheUpdate] instance for the given update document @@ -713,7 +713,7 @@ interface ReactivePanacheMongoRepositoryBase { * Update all entities of this type by the given update document, with named parameters. * The returned [ReactivePanacheUpdate] object will allow to restrict on which document the update should be applied. * - * @param update the update document, if it didn't contain `$set` we add it. + * @param update the update document, if it didn't contain any update operator, we add `$set`. * It can also be expressed as a query string. * @param params [Parameters] of named parameters * @return a new [ReactivePanacheUpdate] instance for the given update document diff --git a/extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/PanacheMongoEntityBase.java b/extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/PanacheMongoEntityBase.java index 3200323b54519..ebb226a3eb107 100755 --- a/extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/PanacheMongoEntityBase.java +++ b/extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/PanacheMongoEntityBase.java @@ -876,7 +876,7 @@ public static void persistOrUpdate(Object firstEntity, Object... entities) { * Update all entities of this type by the given update document, with optional indexed parameters. * The returned {@link PanacheUpdate} object will allow to restrict on which document the update should be applied. * - * @param update the update document, if it didn't contain $set we add it. + * @param update the update document, if it didn't contain any update operator, we add $set.. * It can also be expressed as a {@link io.quarkus.mongodb.panache query string}. * @param params optional sequence of indexed parameters * @return a new {@link PanacheUpdate} instance for the given update document @@ -892,7 +892,7 @@ public static PanacheUpdate update(String update, Object... params) { * Update all entities of this type by the given update document, with named parameters. * The returned {@link PanacheUpdate} object will allow to restrict on which document the update should be applied. * - * @param update the update document, if it didn't contain $set we add it. + * @param update the update document, if it didn't contain any update operator, we add $set. * It can also be expressed as a {@link io.quarkus.mongodb.panache query string}. * @param params {@link Map} of named parameters * @return a new {@link PanacheUpdate} instance for the given update document @@ -909,7 +909,7 @@ public static PanacheUpdate update(String update, Map params) { * Update all entities of this type by the given update document, with named parameters. * The returned {@link PanacheUpdate} object will allow to restrict on which document the update should be applied. * - * @param update the update document, if it didn't contain $set we add it. + * @param update the update document, if it didn't contain any update operator, we add $set. * It can also be expressed as a {@link io.quarkus.mongodb.panache query string}. * @param params {@link Parameters} of named parameters * @return a new {@link PanacheUpdate} instance for the given update document diff --git a/extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/PanacheMongoRepositoryBase.java b/extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/PanacheMongoRepositoryBase.java index 31a39e6692564..eb360d109f827 100755 --- a/extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/PanacheMongoRepositoryBase.java +++ b/extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/PanacheMongoRepositoryBase.java @@ -883,7 +883,7 @@ default void persistOrUpdate(Entity firstEntity, @SuppressWarnings("unchecked") * Update all entities of this type by the given update document, with optional indexed parameters. * The returned {@link PanacheUpdate} object will allow to restrict on which documents the update should be applied. * - * @param update the update document, if it didn't contain $set we add it. + * @param update the update document, if it didn't contain any update operator, we add $set. * It can also be expressed as a {@link io.quarkus.mongodb.panache query string}. * @param params optional sequence of indexed parameters * @return a new {@link PanacheUpdate} instance for the given update document @@ -899,7 +899,7 @@ default PanacheUpdate update(String update, Object... params) { * Update all entities of this type by the given update document, with named parameters. * The returned {@link PanacheUpdate} object will allow to restrict on which documents the update should be applied. * - * @param update the update document, if it didn't contain $set we add it. + * @param update the update document, if it didn't contain any update operator, we add $set. * It can also be expressed as a {@link io.quarkus.mongodb.panache query string}. * @param params {@link Map} of named parameters * @return a new {@link PanacheUpdate} instance for the given update document @@ -916,7 +916,7 @@ default PanacheUpdate update(String update, Map params) { * Update all entities of this type by the given update document, with named parameters. * The returned {@link PanacheUpdate} object will allow to restrict on which document the update should be applied. * - * @param update the update document, if it didn't contain $set we add it. + * @param update the update document, if it didn't contain any update operator, we add $set. * It can also be expressed as a {@link io.quarkus.mongodb.panache query string}. * @param params {@link Parameters} of named parameters * @return a new {@link PanacheUpdate} instance for the given update document diff --git a/extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/reactive/ReactivePanacheMongoEntityBase.java b/extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/reactive/ReactivePanacheMongoEntityBase.java index bf4f7d92645d8..c326e9136b749 100644 --- a/extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/reactive/ReactivePanacheMongoEntityBase.java +++ b/extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/reactive/ReactivePanacheMongoEntityBase.java @@ -889,7 +889,7 @@ public static Uni persistOrUpdate(Object firstEntity, Object... entities) * Update all entities of this type by the given update document, with optional indexed parameters. * The returned {@link PanacheUpdate} object will allow to restrict on which document the update should be applied. * - * @param update the update document, if it didn't contain $set we add it. + * @param update the update document, if it didn't contain any update operator, we add $set. * It can also be expressed as a {@link io.quarkus.mongodb.panache query string}. * @param params optional sequence of indexed parameters * @return a new {@link ReactivePanacheUpdate} instance for the given update document @@ -905,7 +905,7 @@ public static ReactivePanacheUpdate update(String update, Object... params) { * Update all entities of this type by the given update document, with named parameters. * The returned {@link PanacheUpdate} object will allow to restrict on which document the update should be applied. * - * @param update the update document, if it didn't contain $set we add it. + * @param update the update document, if it didn't contain any update operator, we add $set. * It can also be expressed as a {@link io.quarkus.mongodb.panache query string}. * @param params {@link Map} of named parameters * @return a new {@link ReactivePanacheUpdate} instance for the given update document @@ -922,7 +922,7 @@ public static ReactivePanacheUpdate update(String update, Map pa * Update all entities of this type by the given update document, with named parameters. * The returned {@link PanacheUpdate} object will allow to restrict on which document the update should be applied. * - * @param update the update document, if it didn't contain $set we add it. + * @param update the update document, if it didn't contain any update operator, we add $set. * It can also be expressed as a {@link io.quarkus.mongodb.panache query string}. * @param params {@link Parameters} of named parameters * @return a new {@link ReactivePanacheUpdate} instance for the given update document diff --git a/extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/reactive/ReactivePanacheMongoRepositoryBase.java b/extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/reactive/ReactivePanacheMongoRepositoryBase.java index 0e066345f3cdb..8eb0ac42aa98d 100644 --- a/extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/reactive/ReactivePanacheMongoRepositoryBase.java +++ b/extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/reactive/ReactivePanacheMongoRepositoryBase.java @@ -887,7 +887,7 @@ default Uni persistOrUpdate(Entity firstEntity, Entity... entities) { * Update all entities of this type by the given update document, with optional indexed parameters. * The returned {@link PanacheUpdate} object will allow to restrict on which document the update should be applied. * - * @param update the update document, if it didn't contain $set we add it. + * @param update the update document, if it didn't contain any update operator, we add $set. * It can also be expressed as a {@link io.quarkus.mongodb.panache query string}. * @param params optional sequence of indexed parameters * @return a new {@link ReactivePanacheUpdate} instance for the given update document @@ -903,7 +903,7 @@ default ReactivePanacheUpdate update(String update, Object... params) { * Update all entities of this type by the given update document, with named parameters. * The returned {@link PanacheUpdate} object will allow to restrict on which document the update should be applied. * - * @param update the update document, if it didn't contain $set we add it. + * @param update the update document, if it didn't contain any update operator, we add $set. * It can also be expressed as a {@link io.quarkus.mongodb.panache query string}. * @param params {@link Map} of named parameters * @return a new {@link ReactivePanacheUpdate} instance for the given update document @@ -920,7 +920,7 @@ default ReactivePanacheUpdate update(String update, Map params) * Update all entities of this type by the given update document, with named parameters. * The returned {@link PanacheUpdate} object will allow to restrict on which document the update should be applied. * - * @param update the update document, if it didn't contain $set we add it. + * @param update the update document, if it didn't contain any update operator, we add $set. * It can also be expressed as a {@link io.quarkus.mongodb.panache query string}. * @param params {@link Parameters} of named parameters * @return a new {@link ReactivePanacheUpdate} instance for the given update document diff --git a/extensions/panache/mongodb-panache/runtime/src/test/java/io/quarkus/mongodb/panache/runtime/MongoOperationsTest.java b/extensions/panache/mongodb-panache/runtime/src/test/java/io/quarkus/mongodb/panache/runtime/MongoOperationsTest.java index 38e01aad9280f..fe4fc16c589af 100644 --- a/extensions/panache/mongodb-panache/runtime/src/test/java/io/quarkus/mongodb/panache/runtime/MongoOperationsTest.java +++ b/extensions/panache/mongodb-panache/runtime/src/test/java/io/quarkus/mongodb/panache/runtime/MongoOperationsTest.java @@ -369,6 +369,15 @@ public void testBindUpdate() { Parameters.with("field", "a value").map()); assertEquals("{'$set':{'field': 'a value'}}", update); + // native update by index with $inc + update = operations.bindUpdate(DemoObj.class, "{'$inc':{'field': ?1}}", new Object[] { "a value" }); + assertEquals("{'$inc':{'field': 'a value'}}", update); + + // native update by name with $inc + update = operations.bindUpdate(Object.class, "{'$inc':{'field': :field}}", + Parameters.with("field", "a value").map()); + assertEquals("{'$inc':{'field': 'a value'}}", update); + // shortand update update = operations.bindUpdate(Object.class, "field", new Object[] { "a value" }); assertEquals("{'$set':{'field':'a value'}}", update); diff --git a/integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/test/TestImperativeEntity.java b/integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/test/TestImperativeEntity.java index 4dc23313b65b3..051a32df1530a 100644 --- a/integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/test/TestImperativeEntity.java +++ b/integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/test/TestImperativeEntity.java @@ -6,6 +6,7 @@ public class TestImperativeEntity extends PanacheMongoEntity { public String title; public String category; public String description; + public int cpt = 1; public TestImperativeEntity() { } diff --git a/integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/test/TestReactiveEntity.java b/integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/test/TestReactiveEntity.java index 59ff60f8be2c9..edce5d613916c 100644 --- a/integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/test/TestReactiveEntity.java +++ b/integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/test/TestReactiveEntity.java @@ -6,6 +6,7 @@ public class TestReactiveEntity extends ReactivePanacheMongoEntity { public String title; public String category; public String description; + public int cpt = 1; public TestReactiveEntity() { } diff --git a/integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/test/TestResource.java b/integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/test/TestResource.java index a13af23876ce6..41657f6ed6318 100644 --- a/integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/test/TestResource.java +++ b/integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/test/TestResource.java @@ -149,12 +149,18 @@ public Response testImperativeEntity() { updated = TestImperativeEntity.update("{'category' : :category}", Parameters.with("category", "newCategory2")) .where("{'category' : :category}", Parameters.with("category", "newCategory")); Assertions.assertEquals(5, updated); - Assertions.assertEquals(5, TestImperativeEntity.count("category = ?1", "newCategory2")); + updated = TestImperativeEntity.update("{'$set': {'category' : :category}}", Parameters.with("category", "newCategory3")) + .where("{'category' : :category}", Parameters.with("category", "newCategory2")); + Assertions.assertEquals(5, updated); + Assertions.assertEquals(5, TestImperativeEntity.count("category = ?1", "newCategory3")); updated = TestImperativeEntity.update("newField", "newValue").all(); Assertions.assertEquals(10, updated); + updated = TestImperativeEntity.update("{'$inc': {'cpt': 1}}").all(); + Assertions.assertEquals(10, updated); + Assertions.assertEquals(10, TestImperativeEntity.count("cpt = ?1", 2)); // delete - TestImperativeEntity.delete("category = ?1", "newCategory2"); + TestImperativeEntity.delete("category = ?1", "newCategory3"); TestImperativeEntity.delete("{'category' : ?1}", "category1"); Assertions.assertEquals(0, TestImperativeEntity.count()); TestImperativeEntity.persist(entities.stream()); @@ -292,12 +298,19 @@ public Response testImperativeRepository() { updated = testImperativeRepository.update("{'category' : :category}", Parameters.with("category", "newCategory2")) .where("{'category' : :category}", Parameters.with("category", "newCategory")); Assertions.assertEquals(5, updated); - Assertions.assertEquals(5, testImperativeRepository.count("category = ?1", "newCategory2")); + updated = testImperativeRepository + .update("{'$set': {'category' : :category}}", Parameters.with("category", "newCategory3")) + .where("{'category' : :category}", Parameters.with("category", "newCategory2")); + Assertions.assertEquals(5, updated); + Assertions.assertEquals(5, testImperativeRepository.count("category = ?1", "newCategory3")); updated = testImperativeRepository.update("newField", "newValue").all(); Assertions.assertEquals(10, updated); + updated = testImperativeRepository.update("{'$inc': {'cpt': 1}}").all(); + Assertions.assertEquals(10, updated); + Assertions.assertEquals(10, testImperativeRepository.count("cpt = ?1", 2)); // delete - testImperativeRepository.delete("category = ?1", "newCategory2"); + testImperativeRepository.delete("category = ?1", "newCategory3"); testImperativeRepository.delete("{'category' : ?1}", "category1"); Assertions.assertEquals(0, testImperativeRepository.count()); testImperativeRepository.persist(entities.stream()); @@ -515,12 +528,18 @@ public Response testReactiveEntity() { updated = TestReactiveEntity.update("{'category' : :category}", Parameters.with("category", "newCategory2")) .where("{'category' : :category}", Parameters.with("category", "newCategory")).await().indefinitely(); Assertions.assertEquals(5, updated); - Assertions.assertEquals(5, TestReactiveEntity.count("category = ?1", "newCategory2").await().indefinitely()); + updated = TestReactiveEntity.update("{'$set': {'category' : :category}}", Parameters.with("category", "newCategory3")) + .where("{'category' : :category}", Parameters.with("category", "newCategory2")).await().indefinitely(); + Assertions.assertEquals(5, updated); + Assertions.assertEquals(5, TestReactiveEntity.count("category = ?1", "newCategory3").await().indefinitely()); updated = TestReactiveEntity.update("newField", "newValue").all().await().indefinitely(); Assertions.assertEquals(10, updated); + updated = TestReactiveEntity.update("{'$inc': {'cpt': 1}}").all().await().indefinitely(); + Assertions.assertEquals(10, updated); + Assertions.assertEquals(10, TestReactiveEntity.count("cpt = ?1", 2).await().indefinitely()); // delete - TestReactiveEntity.delete("category = ?1", "newCategory2").await().indefinitely(); + TestReactiveEntity.delete("category = ?1", "newCategory3").await().indefinitely(); TestReactiveEntity.delete("{'category' : ?1}", "category1").await().indefinitely(); Assertions.assertEquals(0, TestReactiveEntity.count().await().indefinitely()); TestReactiveEntity.persist(entities.stream()).await().indefinitely(); @@ -668,12 +687,19 @@ public Response testReactiveRepository() { updated = testReactiveRepository.update("{'category' : :category}", Parameters.with("category", "newCategory2")) .where("{'category' : :category}", Parameters.with("category", "newCategory")).await().indefinitely(); Assertions.assertEquals(5, updated); - Assertions.assertEquals(5, testReactiveRepository.count("category = ?1", "newCategory2").await().indefinitely()); + updated = testReactiveRepository + .update("{'$set': {'category' : :category}}", Parameters.with("category", "newCategory3")) + .where("{'category' : :category}", Parameters.with("category", "newCategory2")).await().indefinitely(); + Assertions.assertEquals(5, updated); + Assertions.assertEquals(5, testReactiveRepository.count("category = ?1", "newCategory3").await().indefinitely()); updated = testReactiveRepository.update("newField", "newValue").all().await().indefinitely(); Assertions.assertEquals(10, updated); + updated = testReactiveRepository.update("{'$inc': {'cpt': 1}}").all().await().indefinitely(); + Assertions.assertEquals(10, updated); + Assertions.assertEquals(10, testReactiveRepository.count("cpt = ?1", 2).await().indefinitely()); // delete - testReactiveRepository.delete("category = ?1", "newCategory2").await().indefinitely(); + testReactiveRepository.delete("category = ?1", "newCategory3").await().indefinitely(); testReactiveRepository.delete("{'category' : ?1}", "category1").await().indefinitely(); Assertions.assertEquals(0, testReactiveRepository.count().await().indefinitely()); testReactiveRepository.persist(entities.stream()).await().indefinitely();