onComple
* Executes the given {@link Query} on the entity collection of the specified {@code entityType} backed by a Mongo DB
* {@link com.mongodb.client.FindIterable}.
*
- * Returns a {@link CloseableIterator} that wraps the a Mongo DB {@link com.mongodb.client.FindIterable} that needs to
- * be closed.
+ * Returns a {@link String} that wraps the Mongo DB {@link com.mongodb.client.FindIterable} that needs to be closed.
*
* @param query the query class that specifies the criteria used to find a record and also an optional fields
* specification. Must not be {@literal null}.
* @param entityType must not be {@literal null}.
* @param element return type
- * @return will never be {@literal null}.
+ * @return the result {@link Stream}, containing mapped objects, needing to be closed once fully processed (e.g.
+ * through a try-with-resources clause).
* @since 1.7
*/
- CloseableIterator stream(Query query, Class entityType);
+ Stream 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 com.mongodb.client.FindIterable}.
*
- * Returns a {@link CloseableIterator} that wraps the a Mongo DB {@link com.mongodb.client.FindIterable} that needs to
- * be closed.
+ * Returns a {@link Stream} that wraps the Mongo DB {@link com.mongodb.client.FindIterable} that needs to be closed.
*
* @param query the query class that specifies the criteria used to find a record and also an optional fields
* specification. Must not be {@literal null}.
* @param entityType must not be {@literal null}.
* @param collectionName must not be {@literal null} or empty.
* @param element return type
- * @return will never be {@literal null}.
+ * @return the result {@link Stream}, containing mapped objects, needing to be closed once fully processed (e.g.
+ * through a try-with-resources clause).
* @since 1.10
*/
- CloseableIterator stream(Query query, Class entityType, String collectionName);
+ Stream stream(Query query, Class entityType, String collectionName);
/**
* Create an uncapped collection with a name based on the provided entity class.
@@ -521,9 +522,9 @@ GroupByResults group(@Nullable Criteria criteria, String inputCollectionN
/**
* Execute an aggregation operation backed by a Mongo DB {@link com.mongodb.client.AggregateIterable}.
*
- * Returns a {@link CloseableIterator} that wraps the a Mongo DB {@link com.mongodb.client.AggregateIterable} that
- * needs to be closed. The raw results will be mapped to the given entity class. The name of the inputCollection is
- * derived from the inputType of the aggregation.
+ * Returns a {@link Stream} that wraps the Mongo DB {@link com.mongodb.client.AggregateIterable} that needs to be
+ * closed. The raw results will be mapped to the given entity class. The name of the inputCollection is derived from
+ * the inputType of the aggregation.
*
* Aggregation streaming can't be used with {@link AggregationOptions#isExplain() aggregation explain}. Enabling
* explanation mode will throw an {@link IllegalArgumentException}.
@@ -532,35 +533,37 @@ GroupByResults group(@Nullable Criteria criteria, String inputCollectionN
* {@literal null}.
* @param collectionName The name of the input collection to use for the aggreation.
* @param outputType The parametrized type of the returned list, must not be {@literal null}.
- * @return The results of the aggregation operation.
+ * @return the result {@link Stream}, containing mapped objects, needing to be closed once fully processed (e.g.
+ * through a try-with-resources clause).
* @since 2.0
*/
- CloseableIterator aggregateStream(TypedAggregation> aggregation, String collectionName, Class outputType);
+ Stream aggregateStream(TypedAggregation> aggregation, String collectionName, Class outputType);
/**
* Execute an aggregation operation backed by a Mongo DB {@link com.mongodb.client.AggregateIterable}.
- *
- * Returns a {@link CloseableIterator} that wraps the a Mongo DB {@link com.mongodb.client.AggregateIterable} that
- * needs to be closed. The raw results will be mapped to the given entity class and are returned as stream. The name
- * of the inputCollection is derived from the inputType of the aggregation.
- *
+ *
+ * Returns a {@link Stream} that wraps the Mongo DB {@link com.mongodb.client.AggregateIterable} that needs to be
+ * closed. The raw results will be mapped to the given entity class and are returned as stream. The name of the
+ * inputCollection is derived from the inputType of the aggregation.
+ *
* Aggregation streaming can't be used with {@link AggregationOptions#isExplain() aggregation explain}. Enabling
* explanation mode will throw an {@link IllegalArgumentException}.
*
* @param aggregation The {@link TypedAggregation} specification holding the aggregation operations, must not be
* {@literal null}.
* @param outputType The parametrized type of the returned list, must not be {@literal null}.
- * @return The results of the aggregation operation.
+ * @return the result {@link Stream}, containing mapped objects, needing to be closed once fully processed (e.g.
+ * through a try-with-resources clause).
* @since 2.0
*/
- CloseableIterator aggregateStream(TypedAggregation> aggregation, Class outputType);
+ Stream aggregateStream(TypedAggregation> aggregation, Class outputType);
/**
* Execute an aggregation operation backed by a Mongo DB {@link com.mongodb.client.AggregateIterable}.
- *
- * Returns a {@link CloseableIterator} that wraps the a Mongo DB {@link com.mongodb.client.AggregateIterable} that
- * needs to be closed. The raw results will be mapped to the given entity class.
- *
+ *
+ * Returns a {@link Stream} that wraps the Mongo DB {@link com.mongodb.client.AggregateIterable} that needs to be
+ * closed. The raw results will be mapped to the given entity class.
+ *
* Aggregation streaming can't be used with {@link AggregationOptions#isExplain() aggregation explain}. Enabling
* explanation mode will throw an {@link IllegalArgumentException}.
*
@@ -569,17 +572,18 @@ GroupByResults group(@Nullable Criteria criteria, String inputCollectionN
* @param inputType the inputType where the aggregation operation will read from, must not be {@literal null} or
* empty.
* @param outputType The parametrized type of the returned list, must not be {@literal null}.
- * @return The results of the aggregation operation.
+ * @return the result {@link Stream}, containing mapped objects, needing to be closed once fully processed (e.g.
+ * through a try-with-resources clause).
* @since 2.0
*/
- CloseableIterator aggregateStream(Aggregation aggregation, Class> inputType, Class outputType);
+ Stream aggregateStream(Aggregation aggregation, Class> inputType, Class outputType);
/**
* Execute an aggregation operation backed by a Mongo DB {@link com.mongodb.client.AggregateIterable}.
- *
- * Returns a {@link CloseableIterator} that wraps the a Mongo DB {@link com.mongodb.client.AggregateIterable} that
- * needs to be closed. The raw results will be mapped to the given entity class.
- *
+ *
+ * Returns a {@link Stream} that wraps the Mongo DB {@link com.mongodb.client.AggregateIterable} that needs to be
+ * closed. The raw results will be mapped to the given entity class.
+ *
* Aggregation streaming can't be used with {@link AggregationOptions#isExplain() aggregation explain}. Enabling
* explanation mode will throw an {@link IllegalArgumentException}.
*
@@ -588,10 +592,11 @@ GroupByResults group(@Nullable Criteria criteria, String inputCollectionN
* @param collectionName the collection where the aggregation operation will read from, must not be {@literal null} or
* empty.
* @param outputType The parametrized type of the returned list, must not be {@literal null}.
- * @return The results of the aggregation operation.
+ * @return the result {@link Stream}, containing mapped objects, needing to be closed once fully processed (e.g.
+ * through a try-with-resources clause).
* @since 2.0
*/
- CloseableIterator aggregateStream(Aggregation aggregation, String collectionName, Class outputType);
+ Stream aggregateStream(Aggregation aggregation, String collectionName, Class outputType);
/**
* Execute a map-reduce operation. The map-reduce operation will be formed with an output type of INLINE
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 a07e3fee76..8fb3310ae3 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
@@ -1,5 +1,5 @@
/*
- * Copyright 2010-2021 the original author or authors.
+ * Copyright 2010-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
+import java.util.stream.Stream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -308,10 +309,6 @@ public void setReadPreference(@Nullable ReadPreference readPreference) {
this.readPreference = readPreference;
}
- /*
- * (non-Javadoc)
- * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
- */
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
prepareIndexCreator(applicationContext);
@@ -378,26 +375,18 @@ public MongoConverter getConverter() {
return this.mongoConverter;
}
- /*
- * (non-Javadoc)
- * @see org.springframework.data.mongodb.core.MongoOperations#executeAsStream(org.springframework.data.mongodb.core.query.Query, java.lang.Class)
- */
@Override
- public CloseableIterator stream(Query query, Class entityType) {
+ public Stream stream(Query query, Class entityType) {
return stream(query, entityType, getCollectionName(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(Query query, Class entityType, String collectionName) {
+ public Stream stream(Query query, Class entityType, String collectionName) {
return doStream(query, entityType, collectionName, entityType);
}
@SuppressWarnings("ConstantConditions")
- protected CloseableIterator doStream(Query query, Class> entityType, String collectionName,
+ protected Stream doStream(Query query, Class> entityType, String collectionName,
Class returnType) {
Assert.notNull(query, "Query must not be null!");
@@ -405,7 +394,7 @@ protected CloseableIterator doStream(Query query, Class> entityType, St
Assert.hasText(collectionName, "Collection name must not be null or empty!");
Assert.notNull(returnType, "ReturnType must not be null!");
- return execute(collectionName, (CollectionCallback>) collection -> {
+ return execute(collectionName, (CollectionCallback>) collection -> {
MongoPersistentEntity> persistentEntity = mappingContext.getPersistentEntity(entityType);
@@ -420,7 +409,7 @@ protected CloseableIterator doStream(Query query, Class> entityType, St
col -> col.find(mappedQuery, Document.class).projection(mappedFields));
return new CloseableIterableCursorAdapter<>(cursor, exceptionTranslator,
- new ProjectingReadCallback<>(mongoConverter, projection, collectionName));
+ new ProjectingReadCallback<>(mongoConverter, projection, collectionName)).stream();
});
}
@@ -429,10 +418,6 @@ public String getCollectionName(Class> entityClass) {
return this.operations.determineCollectionName(entityClass);
}
- /*
- * (non-Javadoc)
- * @see org.springframework.data.mongodb.core.MongoOperations#executeCommand(java.lang.String)
- */
@Override
@SuppressWarnings("ConstantConditions")
public Document executeCommand(String jsonCommand) {
@@ -442,10 +427,6 @@ public Document executeCommand(String jsonCommand) {
return execute(db -> db.runCommand(Document.parse(jsonCommand), Document.class));
}
- /*
- * (non-Javadoc)
- * @see org.springframework.data.mongodb.core.MongoOperations#executeCommand(org.bson.Document)
- */
@Override
@SuppressWarnings("ConstantConditions")
public Document executeCommand(Document command) {
@@ -455,11 +436,6 @@ public Document executeCommand(Document command) {
return execute(db -> db.runCommand(command, Document.class));
}
- /*
- * (non-Javadoc)
- * @see org.springframework.data.mongodb.core.MongoOperations#executeCommand(org.bson.Document, com.mongodb.ReadPreference)
- */
-
@Override
@SuppressWarnings("ConstantConditions")
public Document executeCommand(Document command, @Nullable ReadPreference readPreference) {
@@ -471,10 +447,6 @@ public Document executeCommand(Document command, @Nullable ReadPreference readPr
: db.runCommand(command, Document.class));
}
- /*
- * (non-Javadoc)
- * @see org.springframework.data.mongodb.core.MongoOperations#executeQuery(org.springframework.data.mongodb.core.query.Query, java.lang.String, org.springframework.data.mongodb.core.DocumentCallbackHandler)
- */
@Override
public void executeQuery(Query query, String collectionName, DocumentCallbackHandler dch) {
executeQuery(query, collectionName, dch, new QueryCursorPreparer(query, null));
@@ -511,10 +483,6 @@ protected void executeQuery(Query query, String collectionName, DocumentCallback
preparer != null ? preparer : CursorPreparer.NO_OP_PREPARER, documentCallbackHandler, collectionName);
}
- /*
- * (non-Javadoc)
- * @see org.springframework.data.mongodb.core.MongoOperations#execute(org.springframework.data.mongodb.core.DbCallback)
- */
public T execute(DbCallback action) {
Assert.notNull(action, "DbCallback must not be null!");
@@ -527,20 +495,12 @@ public T execute(DbCallback action) {
}
}
- /*
- * (non-Javadoc)
- * @see org.springframework.data.mongodb.core.MongoOperations#execute(java.lang.Class, org.springframework.data.mongodb.core.DbCallback)
- */
public T execute(Class> entityClass, CollectionCallback callback) {
Assert.notNull(entityClass, "EntityClass must not be null!");
return execute(getCollectionName(entityClass), callback);
}
- /*
- * (non-Javadoc)
- * @see org.springframework.data.mongodb.core.MongoOperations#execute(java.lang.String, org.springframework.data.mongodb.core.DbCallback)
- */
public T execute(String collectionName, CollectionCallback callback) {
Assert.notNull(collectionName, "CollectionName must not be null!");
@@ -554,10 +514,6 @@ public T execute(String collectionName, CollectionCallback callback) {
}
}
- /*
- * (non-Javadoc)
- * @see org.springframework.data.mongodb.core.MongoOperations#withSession(com.mongodb.ClientSessionOptions)
- */
@Override
public SessionScoped withSession(ClientSessionOptions options) {
@@ -566,10 +522,6 @@ public SessionScoped withSession(ClientSessionOptions options) {
return withSession(() -> mongoDbFactory.getSession(options));
}
- /*
- * (non-Javadoc)
- * @see org.springframework.data.mongodb.core.MongoOperations#withSession(com.mongodb.session.ClientSession)
- */
@Override
public MongoTemplate withSession(ClientSession session) {
@@ -589,18 +541,10 @@ public void setSessionSynchronization(SessionSynchronization sessionSynchronizat
this.sessionSynchronization = sessionSynchronization;
}
- /*
- * (non-Javadoc)
- * @see org.springframework.data.mongodb.core.MongoOperations#createCollection(java.lang.Class)
- */
public MongoCollection createCollection(Class entityClass) {
return createCollection(entityClass, operations.forType(entityClass).getCollectionOptions());
}
- /*
- * (non-Javadoc)
- * @see org.springframework.data.mongodb.core.MongoOperations#createCollection(java.lang.Class, org.springframework.data.mongodb.core.CollectionOptions)
- */
public MongoCollection createCollection(Class entityClass,
@Nullable CollectionOptions collectionOptions) {
@@ -615,10 +559,6 @@ public MongoCollection createCollection(Class entityClass,
return doCreateCollection(getCollectionName(entityClass), convertToDocument(options, entityClass));
}
- /*
- * (non-Javadoc)
- * @see org.springframework.data.mongodb.core.MongoOperations#createCollection(java.lang.String)
- */
public MongoCollection createCollection(String collectionName) {
Assert.notNull(collectionName, "CollectionName must not be null!");
@@ -626,10 +566,6 @@ public MongoCollection createCollection(String collectionName) {
return doCreateCollection(collectionName, new Document());
}
- /*
- * (non-Javadoc)
- * @see org.springframework.data.mongodb.core.MongoOperations#createCollection(java.lang.String, org.springframework.data.mongodb.core.CollectionOptions)
- */
public MongoCollection createCollection(String collectionName,
@Nullable CollectionOptions collectionOptions) {
@@ -637,10 +573,6 @@ public MongoCollection createCollection(String collectionName,
return doCreateCollection(collectionName, convertToDocument(collectionOptions));
}
- /*
- * (non-Javadoc)
- * @see org.springframework.data.mongodb.core.MongoOperations#getCollection(java.lang.String)
- */
@SuppressWarnings("ConstantConditions")
public MongoCollection getCollection(String collectionName) {
@@ -649,18 +581,10 @@ public MongoCollection getCollection(String collectionName) {
return execute(db -> db.getCollection(collectionName, Document.class));
}
- /*
- * (non-Javadoc)
- * @see org.springframework.data.mongodb.core.ExecutableInsertOperation#getCollection(java.lang.Class)
- */
public boolean collectionExists(Class