diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoOperations.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoOperations.java index fa878b31a..35bfe05ca 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoOperations.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoOperations.java @@ -126,8 +126,9 @@ public interface MongoOperations { * Create a collect with the provided name and options. * @param collectionName name of the collection * @param collectionOptions options to use when creating the collection. + * @return the created collection */ - void createCollection(String collectionName, CollectionOptions collectionOptions); + DBCollection createCollection(String collectionName, CollectionOptions collectionOptions); /** * A set of collection names. @@ -426,6 +427,21 @@ public interface MongoOperations { */ void insert(String collectionName, Object objectToSave); + /** + * Insert the object into the default collection. + * + * The object is converted to the MongoDB native representation using an instance of + * {@see MongoWriter} + * + * Insert is used to initially store the object into the + * database. To update an existing object use the save method. + * + * @param the type of the object to insert + * @param objectToSave the object to store in the collection + * @param writer the writer to convert the object to save into a DBObject + */ + void insert(T objectToSave, MongoWriter writer); + /** * Insert the object into the specified collection. * @@ -456,6 +472,15 @@ public interface MongoOperations { */ void insertList(String collectionName, List listToSave); + /** + * Insert a list of objects into the default collection using the provided MongoWriter instance + * + * @param the type of object being saved + * @param listToSave the list of objects to save. + * @param writer the writer to convert the object to save into a DBObject + */ + void insertList(List listToSave, MongoWriter writer); + /** * Insert a list of objects into the specified collection using the provided MongoWriter instance * @@ -506,7 +531,22 @@ public interface MongoOperations { void save(String collectionName, Object objectToSave); /** - * Save the object into the specified collection. This will perform an insert if the object is not already + * Save the object into the default collection using the provided writer. + * This will perform an insert if the object is not already + * present, that is an 'upsert'. + * + * The object is converted to the MongoDB native representation using an instance of + * {@see MongoWriter} + * + * @param the type of the object to insert + * @param objectToSave the object to store in the collection + * @param writer the writer to convert the object to save into a DBObject + */ + void save(T objectToSave, MongoWriter writer); + + /** + * Save the object into the specified collection using the provided writer. + * This will perform an insert if the object is not already * present, that is an 'upsert'. * * The object is converted to the MongoDB native representation using an instance of @@ -564,13 +604,13 @@ public interface MongoOperations { Update update); /** - * Remove all documents from the default collection that match the provide query document criteria. + * Remove all documents from the default collection that match the provided query document criteria. * @param queryDoc the query document that specifies the criteria used to remove a record */ void remove(Query query); /** - * Remove all documents from the specified collection that match the provide query document criteria. + * Remove all documents from the specified collection that match the provided query document criteria. * @param collectionName name of the collection where the objects will removed * @param queryDoc the query document that specifies the criteria used to remove a record */ diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java index 6ac24e065..44817a600 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java @@ -378,23 +378,14 @@ public class MongoTemplate implements InitializingBean, MongoOperations { * @see org.springframework.data.document.mongodb.MongoOperations#createCollection(java.lang.String) */ public DBCollection createCollection(final String collectionName) { - return execute(new DbCallback() { - public DBCollection doInDB(DB db) throws MongoException, DataAccessException { - return db.createCollection(collectionName, new BasicDBObject()); - } - }); + return doCreateCollection(collectionName, new BasicDBObject()); } /* (non-Javadoc) * @see org.springframework.data.document.mongodb.MongoOperations#createCollection(java.lang.String, org.springframework.data.document.mongodb.CollectionOptions) */ - public void createCollection(final String collectionName, final CollectionOptions collectionOptions) { - execute(new DbCallback() { - public Void doInDB(DB db) throws MongoException, DataAccessException { - db.createCollection(collectionName, convertToDbObject(collectionOptions)); - return null; - } - }); + public DBCollection createCollection(final String collectionName, final CollectionOptions collectionOptions) { + return doCreateCollection(collectionName, convertToDbObject(collectionOptions)); } /* (non-Javadoc) @@ -535,6 +526,13 @@ public class MongoTemplate implements InitializingBean, MongoOperations { insert(collectionName, objectToSave, this.mongoConverter); } + /* (non-Javadoc) + * @see org.springframework.data.document.mongodb.MongoOperations#insert(T, org.springframework.data.document.mongodb.MongoWriter) + */ + public void insert(T objectToSave, MongoWriter writer) { + insert(getDefaultCollectionName(), objectToSave, writer); + } + /* (non-Javadoc) * @see org.springframework.data.document.mongodb.MongoOperations#insert(java.lang.String, T, org.springframework.data.document.mongodb.MongoWriter) */ @@ -559,6 +557,13 @@ public class MongoTemplate implements InitializingBean, MongoOperations { insertList(collectionName, listToSave, this.mongoConverter); } + /* (non-Javadoc) + * @see org.springframework.data.document.mongodb.MongoOperations#insertList(java.util.List, org.springframework.data.document.mongodb.MongoWriter) + */ + public void insertList(List listToSave, MongoWriter writer) { + insertList(getDefaultCollectionName(), listToSave, writer); + } + /* (non-Javadoc) * @see org.springframework.data.document.mongodb.MongoOperations#insertList(java.lang.String, java.util.List, org.springframework.data.document.mongodb.MongoWriter) */ @@ -594,6 +599,13 @@ public class MongoTemplate implements InitializingBean, MongoOperations { save(collectionName, objectToSave, this.mongoConverter); } + /* (non-Javadoc) + * @see org.springframework.data.document.mongodb.MongoOperations#save(T, org.springframework.data.document.mongodb.MongoWriter) + */ + public void save(T objectToSave, MongoWriter writer) { + save(getDefaultCollectionName(), objectToSave, writer); + } + /* (non-Javadoc) * @see org.springframework.data.document.mongodb.MongoOperations#save(java.lang.String, T, org.springframework.data.document.mongodb.MongoWriter) */ @@ -624,9 +636,6 @@ public class MongoTemplate implements InitializingBean, MongoOperations { }, collectionName); } - - - protected List insertDBObjectList(String collectionName, final List dbDocList) { if (dbDocList.isEmpty()) { @@ -787,6 +796,20 @@ public class MongoTemplate implements InitializingBean, MongoOperations { return MongoDbUtils.getDB(mongo, databaseName, username, password == null ? null : password.toCharArray()); } + /** + * Create the specified collection using the provided options + * @param collectionName + * @param collectionOptions + * @return the collection that was created + */ + protected DBCollection doCreateCollection(final String collectionName, final DBObject collectionOptions) { + return execute(new DbCallback() { + public DBCollection doInDB(DB db) throws MongoException, DataAccessException { + return db.createCollection(collectionName, collectionOptions); + } + }); + } + /** * Map the results of an ad-hoc query on the default MongoDB collection to an object using the provided MongoReader *