Skip to content

Commit 325bcd1

Browse files
committed
DATAMONGO-1431 - Added MongoOperations.stream(…) with explicit collection.
1 parent 9db2dde commit 325bcd1

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,28 @@ public interface MongoOperations {
175175
* Returns a {@link CloseableIterator} that wraps the a Mongo DB {@link Cursor} that needs to be closed.
176176
*
177177
* @param <T> element return type
178-
* @param query
179-
* @param entityType
180-
* @return
178+
* @param query must not be {@literal null}.
179+
* @param entityType must not be {@literal null}.
180+
* @return will never be {@literal null}.
181181
* @since 1.7
182182
*/
183183
<T> CloseableIterator<T> stream(Query query, Class<T> entityType);
184184

185+
/**
186+
* Executes the given {@link Query} on the entity collection of the specified {@code entityType} and collection backed
187+
* by a Mongo DB {@link Cursor}.
188+
* <p>
189+
* Returns a {@link CloseableIterator} that wraps the a Mongo DB {@link Cursor} that needs to be closed.
190+
*
191+
* @param <T> element return type
192+
* @param query must not be {@literal null}.
193+
* @param entityType must not be {@literal null}.
194+
* @param collectionName must not be {@literal null} or empty.
195+
* @return will never be {@literal null}.
196+
* @since 1.10
197+
*/
198+
<T> CloseableIterator<T> stream(Query query, Class<T> entityType, String collectionName);
199+
185200
/**
186201
* Create an uncapped collection with a name based on the provided entity class.
187202
*
@@ -1031,5 +1046,4 @@ <T> T findAndModify(Query query, Update update, FindAndModifyOptions options, Cl
10311046
* @return
10321047
*/
10331048
MongoConverter getConverter();
1034-
10351049
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,21 @@ public MongoConverter getConverter() {
326326
@Override
327327
public <T> CloseableIterator<T> stream(final Query query, final Class<T> entityType) {
328328

329-
return execute(entityType, new CollectionCallback<CloseableIterator<T>>() {
329+
return stream(query, entityType, determineCollectionName(entityType));
330+
}
331+
332+
/*
333+
* (non-Javadoc)
334+
* @see org.springframework.data.mongodb.core.MongoOperations#stream(org.springframework.data.mongodb.core.query.Query, java.lang.Class, java.lang.String)
335+
*/
336+
@Override
337+
public <T> CloseableIterator<T> stream(final Query query, final Class<T> entityType, String collectionName) {
338+
339+
Assert.notNull(query, "Query must not be null!");
340+
Assert.notNull(entityType, "Entity type must not be null!");
341+
Assert.hasText(collectionName, "Collection name must not be null or empty!");
342+
343+
return execute(collectionName, new CollectionCallback<CloseableIterator<T>>() {
330344

331345
@Override
332346
public CloseableIterator<T> doInCollection(DBCollection collection) throws MongoException, DataAccessException {

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3344,6 +3344,30 @@ public void updatesBigNumberValueUsingStringComparisonWhenUsingMinOperator() {
33443344
assertThat(loaded.bigDeciamVal, equalTo(new BigDecimal("800")));
33453345
}
33463346

3347+
/**
3348+
* @see DATAMONGO-1431
3349+
*/
3350+
@Test
3351+
public void streamExecutionUsesExplicitCollectionName() {
3352+
3353+
template.remove(new Query(), "some_special_collection");
3354+
template.remove(new Query(), Document.class);
3355+
3356+
Document document = new Document();
3357+
3358+
template.insert(document, "some_special_collection");
3359+
3360+
CloseableIterator<Document> stream = template.stream(new Query(), Document.class);
3361+
3362+
assertThat(stream.hasNext(), is(false));
3363+
3364+
stream = template.stream(new Query(), Document.class, "some_special_collection");
3365+
3366+
assertThat(stream.hasNext(), is(true));
3367+
assertThat(stream.next().id, is(document.id));
3368+
assertThat(stream.hasNext(), is(false));
3369+
}
3370+
33473371
static class TypeWithNumbers {
33483372

33493373
@Id String id;

0 commit comments

Comments
 (0)