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 587286170..121f7ac49 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 @@ -235,6 +235,43 @@ public class MongoTemplate implements InitializingBean { } + public void insert(Object objectToSave) { + insert(getRequiredDefaultCollectionName(), objectToSave); + } + + public void insert(String collectionName, Object objectToSave) { + insert(collectionName, objectToSave, this.mongoConverter); + } + + public void insert(String collectionName, T objectToSave, MongoWriter writer) { + BasicDBObject dbDoc = new BasicDBObject(); + writer.write(objectToSave, dbDoc); + Object _id = insertDBObject(collectionName, dbDoc); + populateIdIfNecessary(objectToSave, _id); + } + + public void insertList(List listToSave) { + insertList(getRequiredDefaultCollectionName(), listToSave); + } + + public void insertList(String collectionName, List listToSave) { + insertList(collectionName, listToSave, this.mongoConverter); + } + + public void insertList(String collectionName, List listToSave, MongoWriter writer) { + List dbObjectList = new ArrayList(); + for (T o : listToSave) { + BasicDBObject dbDoc = new BasicDBObject(); + writer.write(o, dbDoc); + dbObjectList.add(dbDoc); + } + List _ids = insertDBObjectList(collectionName, dbObjectList); + for (int i = 0; i < listToSave.size(); i++) { + if (i < _ids.size()) + populateIdIfNecessary(listToSave.get(i), _ids.get(i)); + } + } + public void save(Object objectToSave) { save(getRequiredDefaultCollectionName(), objectToSave); } @@ -251,7 +288,41 @@ public class MongoTemplate implements InitializingBean { } - protected Object saveDBObject(String collectionName, BasicDBObject dbDoc) { + protected Object insertDBObject(String collectionName, DBObject dbDoc) { + if (dbDoc.keySet().size() > 0 ) { + WriteResult wr = null; + try { + wr = getDb().getCollection(collectionName).insert(dbDoc); + return dbDoc.get("_id"); + } catch (MongoException e) { + throw new DataRetrievalFailureException(wr.getLastError().getErrorMessage(), e); + } + } + else { + return null; + } + } + + protected List insertDBObjectList(String collectionName, List dbDocList) { + if (dbDocList.size() > 0 ) { + List ids = new ArrayList(); + WriteResult wr = null; + try { + wr = getDb().getCollection(collectionName).insert(dbDocList); + for (DBObject dbo : dbDocList) { + ids.add(dbo.get("_id")); + } + return ids; + } catch (MongoException e) { + throw new DataRetrievalFailureException(wr.getLastError().getErrorMessage(), e); + } + } + else { + return null; + } + } + + protected Object saveDBObject(String collectionName, DBObject dbDoc) { if (dbDoc.keySet().size() > 0 ) { WriteResult wr = null; try {