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 b63c9e8b3..6d3e13d4a 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 @@ -175,13 +175,28 @@ public interface MongoOperations { * Returns a {@link CloseableIterator} that wraps the a Mongo DB {@link Cursor} that needs to be closed. * * @param element return type - * @param query - * @param entityType - * @return + * @param query must not be {@literal null}. + * @param entityType must not be {@literal null}. + * @return will never be {@literal null}. * @since 1.7 */ CloseableIterator stream(Query query, Class entityType); + /** + * Executes the given {@link Query} on the entity collection of the specified {@code entityType} and collection backed + * by a Mongo DB {@link Cursor}. + *

+ * Returns a {@link CloseableIterator} that wraps the a Mongo DB {@link Cursor} that needs to be closed. + * + * @param element return type + * @param query must not be {@literal null}. + * @param entityType must not be {@literal null}. + * @param collectionName must not be {@literal null} or empty. + * @return will never be {@literal null}. + * @since 1.10 + */ + CloseableIterator stream(Query query, Class entityType, String collectionName); + /** * Create an uncapped collection with a name based on the provided entity class. * @@ -1031,5 +1046,4 @@ public interface MongoOperations { * @return */ MongoConverter getConverter(); - } 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 e7419a06f..bbb9ffc09 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 @@ -326,7 +326,21 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { @Override public CloseableIterator stream(final Query query, final Class entityType) { - return execute(entityType, new CollectionCallback>() { + return stream(query, entityType, determineCollectionName(entityType)); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.mongodb.core.MongoOperations#stream(org.springframework.data.mongodb.core.query.Query, java.lang.Class, java.lang.String) + */ + @Override + public CloseableIterator stream(final Query query, final Class entityType, String collectionName) { + + Assert.notNull(query, "Query must not be null!"); + Assert.notNull(entityType, "Entity type must not be null!"); + Assert.hasText(collectionName, "Collection name must not be null or empty!"); + + return execute(collectionName, new CollectionCallback>() { @Override public CloseableIterator doInCollection(DBCollection collection) throws MongoException, DataAccessException { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java index 43ba0fe8c..ebb99b174 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java @@ -3344,6 +3344,30 @@ public class MongoTemplateTests { assertThat(loaded.bigDeciamVal, equalTo(new BigDecimal("800"))); } + /** + * @see DATAMONGO-1431 + */ + @Test + public void streamExecutionUsesExplicitCollectionName() { + + template.remove(new Query(), "some_special_collection"); + template.remove(new Query(), Document.class); + + Document document = new Document(); + + template.insert(document, "some_special_collection"); + + CloseableIterator stream = template.stream(new Query(), Document.class); + + assertThat(stream.hasNext(), is(false)); + + stream = template.stream(new Query(), Document.class, "some_special_collection"); + + assertThat(stream.hasNext(), is(true)); + assertThat(stream.next().id, is(document.id)); + assertThat(stream.hasNext(), is(false)); + } + static class TypeWithNumbers { @Id String id;