From 5d2fc31164a1c6e7ec525959282d55c11216e27c Mon Sep 17 00:00:00 2001 From: Chuong Ngo Date: Fri, 2 Aug 2013 22:40:04 -0400 Subject: [PATCH] DATAMONGO-738 - Allow to pass collectionName along with entityClass as parameter to update methods in MongoTemplate. Added appropriate overloaded methods to MongoOperations and MongoTemplate. Applied pull request from Chuong Ngo . Original pull request: #57. --- .../data/mongodb/core/MongoOperations.java | 39 +++++++++++++++++++ .../data/mongodb/core/MongoTemplate.java | 13 +++++++ 2 files changed, 52 insertions(+) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java index 2399ee2a4..b5ae39ee3 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java @@ -49,6 +49,7 @@ import com.mongodb.WriteResult; * @author Mark Pollack * @author Oliver Gierke * @author Tobias Trelle + * @author Chuong Ngo */ public interface MongoOperations { @@ -703,6 +704,18 @@ public interface MongoOperations { */ WriteResult upsert(Query query, Update update, String collectionName); + /** + * Performs an upsert. If no document is found that matches the query, a new document is created and inserted by + * combining the query document and the update document. + * + * @param query the query document that specifies the criteria used to select a record to be upserted + * @param update the update document that contains the updated object or $ operators to manipulate the existing object + * @param entityClass class of the pojo to be operated on + * @param collectionName name of the collection to update the object in + * @return the WriteResult which lets you access the results of the previous write. + */ + WriteResult upsert(Query query, Update update, Class entityClass, String collectionName); + /** * Updates the first object that is found in the collection of the entity class that matches the query document with * the provided update document. @@ -727,6 +740,19 @@ public interface MongoOperations { */ WriteResult updateFirst(Query query, Update update, String collectionName); + /** + * Updates the first object that is found in the specified collection that matches the query document criteria with + * the provided updated document. + * + * @param query the query document that specifies the criteria used to select a record to be updated + * @param update the update document that contains the updated object or $ operators to manipulate the existing + * object. + * @param entityClass class of the pojo to be operated on + * @param collectionName name of the collection to update the object in + * @return the WriteResult which lets you access the results of the previous write. + */ + WriteResult updateFirst(Query query, Update update, Class entityClass, String collectionName); + /** * Updates all objects that are found in the collection for the entity class that matches the query document criteria * with the provided updated document. @@ -751,6 +777,19 @@ public interface MongoOperations { */ WriteResult updateMulti(Query query, Update update, String collectionName); + /** + * Updates all objects that are found in the collection for the entity class that matches the query document criteria + * with the provided updated document. + * + * @param query the query document that specifies the criteria used to select a record to be updated + * @param update the update document that contains the updated object or $ operators to manipulate the existing + * object. + * @param entityClass class of the pojo to be operated on + * @param collectionName name of the collection to update the object in + * @return the WriteResult which lets you access the results of the previous write. + */ + WriteResult updateMulti(final Query query, final Update update, Class entityClass, String collectionName); + /** * Remove the given object from the collection by id. * diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java index 2dff69a9c..d288cd00d 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java @@ -123,6 +123,7 @@ import com.mongodb.util.JSONParseException; * @author Tobias Trelle * @author Sebastian Herold * @author Thomas Darimont + * @author Chuong Ngo */ public class MongoTemplate implements MongoOperations, ApplicationContextAware { @@ -928,6 +929,10 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { return doUpdate(collectionName, query, update, null, true, false); } + public WriteResult upsert(Query query, Update update, Class entityClass, String collectionName) { + return doUpdate(collectionName, query, update, entityClass, true, false); + } + public WriteResult updateFirst(Query query, Update update, Class entityClass) { return doUpdate(determineCollectionName(entityClass), query, update, entityClass, false, false); } @@ -936,6 +941,10 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { return doUpdate(collectionName, query, update, null, false, false); } + public WriteResult updateFirst(Query query, Update update, Class entityClass, String collectionName) { + return doUpdate(collectionName, query, update, entityClass, false, false); + } + public WriteResult updateMulti(Query query, Update update, Class entityClass) { return doUpdate(determineCollectionName(entityClass), query, update, entityClass, false, true); } @@ -944,6 +953,10 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { return doUpdate(collectionName, query, update, null, false, true); } + public WriteResult updateMulti(final Query query, final Update update, Class entityClass, String collectionName) { + return doUpdate(collectionName, query, update, entityClass, false, true); + } + protected WriteResult doUpdate(final String collectionName, final Query query, final Update update, final Class entityClass, final boolean upsert, final boolean multi) {