public class MongoTemplate extends Object implements MongoOperations, ApplicationContextAware, IndexOperationsProvider
MongoOperations.ExecutableFindOperation.DistinctWithProjection, ExecutableFindOperation.DistinctWithQuery<T>, ExecutableFindOperation.ExecutableFind<T>, ExecutableFindOperation.FindDistinct, ExecutableFindOperation.FindWithCollection<T>, ExecutableFindOperation.FindWithProjection<T>, ExecutableFindOperation.FindWithQuery<T>, ExecutableFindOperation.TerminatingDistinct<T>, ExecutableFindOperation.TerminatingFind<T>, ExecutableFindOperation.TerminatingFindNear<T>ExecutableInsertOperation.ExecutableInsert<T>, ExecutableInsertOperation.InsertWithBulkMode<T>, ExecutableInsertOperation.InsertWithCollection<T>, ExecutableInsertOperation.TerminatingBulkInsert<T>, ExecutableInsertOperation.TerminatingInsert<T>ExecutableUpdateOperation.ExecutableUpdate<T>, ExecutableUpdateOperation.FindAndModifyWithOptions<T>, ExecutableUpdateOperation.FindAndReplaceWithOptions<T>, ExecutableUpdateOperation.FindAndReplaceWithProjection<T>, ExecutableUpdateOperation.TerminatingFindAndModify<T>, ExecutableUpdateOperation.TerminatingFindAndReplace<T>, ExecutableUpdateOperation.TerminatingUpdate<T>, ExecutableUpdateOperation.UpdateWithCollection<T>, ExecutableUpdateOperation.UpdateWithQuery<T>, ExecutableUpdateOperation.UpdateWithUpdate<T>ExecutableRemoveOperation.ExecutableRemove<T>, ExecutableRemoveOperation.RemoveWithCollection<T>, ExecutableRemoveOperation.RemoveWithQuery<T>, ExecutableRemoveOperation.TerminatingRemove<T>ExecutableAggregationOperation.AggregationWithAggregation<T>, ExecutableAggregationOperation.AggregationWithCollection<T>, ExecutableAggregationOperation.ExecutableAggregation<T>, ExecutableAggregationOperation.TerminatingAggregation<T>ExecutableMapReduceOperation.ExecutableMapReduce<T>, ExecutableMapReduceOperation.MapReduceWithCollection<T>, ExecutableMapReduceOperation.MapReduceWithMapFunction<T>, ExecutableMapReduceOperation.MapReduceWithOptions<T>, ExecutableMapReduceOperation.MapReduceWithProjection<T>, ExecutableMapReduceOperation.MapReduceWithQuery<T>, ExecutableMapReduceOperation.MapReduceWithReduceFunction<T>, ExecutableMapReduceOperation.TerminatingMapReduce<T>| Constructor and Description |
|---|
MongoTemplate(com.mongodb.client.MongoClient mongoClient,
String databaseName)
Constructor used for a basic template configuration.
|
MongoTemplate(MongoDatabaseFactory mongoDbFactory)
Constructor used for a basic template configuration.
|
MongoTemplate(MongoDatabaseFactory mongoDbFactory,
MongoConverter mongoConverter)
Constructor used for a basic template configuration.
|
| Modifier and Type | Method and Description |
|---|---|
<O> AggregationResults<O> |
aggregate(Aggregation aggregation,
Class<?> inputType,
Class<O> outputType)
Execute an aggregation operation.
|
<O> AggregationResults<O> |
aggregate(Aggregation aggregation,
String collectionName,
Class<O> outputType)
Execute an aggregation operation.
|
protected <O> AggregationResults<O> |
aggregate(Aggregation aggregation,
String collectionName,
Class<O> outputType,
AggregationOperationContext context) |
<O> AggregationResults<O> |
aggregate(TypedAggregation<?> aggregation,
Class<O> outputType)
Execute an aggregation operation.
|
<O> AggregationResults<O> |
aggregate(TypedAggregation<?> aggregation,
String inputCollectionName,
Class<O> outputType)
Execute an aggregation operation.
|
<T> ExecutableAggregationOperation.ExecutableAggregation<T> |
aggregateAndReturn(Class<T> domainType)
Start creating an aggregation operation that returns results mapped to the given domain type.
|
<O> CloseableIterator<O> |
aggregateStream(Aggregation aggregation,
Class<?> inputType,
Class<O> outputType)
Execute an aggregation operation backed by a Mongo DB
AggregateIterable. |
<O> CloseableIterator<O> |
aggregateStream(Aggregation aggregation,
String collectionName,
Class<O> outputType)
Execute an aggregation operation backed by a Mongo DB
AggregateIterable. |
protected <O> CloseableIterator<O> |
aggregateStream(Aggregation aggregation,
String collectionName,
Class<O> outputType,
AggregationOperationContext context) |
<O> CloseableIterator<O> |
aggregateStream(TypedAggregation<?> aggregation,
Class<O> outputType)
Execute an aggregation operation backed by a Mongo DB
AggregateIterable. |
<O> CloseableIterator<O> |
aggregateStream(TypedAggregation<?> aggregation,
String inputCollectionName,
Class<O> outputType)
Execute an aggregation operation backed by a Mongo DB
AggregateIterable. |
BulkOperations |
bulkOps(BulkOperations.BulkMode bulkMode,
Class<?> entityClass)
Returns a new
BulkOperations for the given entity type. |
BulkOperations |
bulkOps(BulkOperations.BulkMode mode,
Class<?> entityType,
String collectionName)
Returns a new
BulkOperations for the given entity type and collection name. |
BulkOperations |
bulkOps(BulkOperations.BulkMode bulkMode,
String collectionName)
Returns a new
BulkOperations for the given collection. |
<T> boolean |
collectionExists(Class<T> entityClass)
Check to see if a collection with a name indicated by the entity class exists.
|
boolean |
collectionExists(String collectionName)
Check to see if a collection with a given name exists.
|
protected org.bson.Document |
convertToDocument(CollectionOptions collectionOptions)
Deprecated.
since 2.1 in favor of
convertToDocument(CollectionOptions, Class). |
protected org.bson.Document |
convertToDocument(CollectionOptions collectionOptions,
Class<?> targetType)
Convert given
CollectionOptions to a document and take the domain type information into account when
creating a mapped schema for validation. |
long |
count(Query query,
Class<?> entityClass)
Returns the number of documents for the given
Query by querying the collection of the given entity class. |
long |
count(Query query,
Class<?> entityClass,
String collectionName)
|
long |
count(Query query,
String collectionName)
Returns the number of documents for the given
Query querying the given collection. |
<T> com.mongodb.client.MongoCollection<org.bson.Document> |
createCollection(Class<T> entityClass)
Create an uncapped collection with a name based on the provided entity class.
|
<T> com.mongodb.client.MongoCollection<org.bson.Document> |
createCollection(Class<T> entityClass,
CollectionOptions collectionOptions)
Create a collection with a name based on the provided entity class using the options.
|
com.mongodb.client.MongoCollection<org.bson.Document> |
createCollection(String collectionName)
Create an uncapped collection with the provided name.
|
com.mongodb.client.MongoCollection<org.bson.Document> |
createCollection(String collectionName,
CollectionOptions collectionOptions)
Create a collection with the provided name and options.
|
protected <O> AggregationResults<O> |
doAggregate(Aggregation aggregation,
String collectionName,
Class<O> outputType,
AggregationOperationContext context) |
protected long |
doCount(String collectionName,
org.bson.Document filter,
com.mongodb.client.model.CountOptions options) |
protected com.mongodb.client.MongoCollection<org.bson.Document> |
doCreateCollection(String collectionName,
org.bson.Document collectionOptions)
Create the specified collection using the provided options
|
protected long |
doEstimatedCount(String collectionName,
com.mongodb.client.model.EstimatedDocumentCountOptions options) |
protected <S,T> List<T> |
doFind(String collectionName,
org.bson.Document query,
org.bson.Document fields,
Class<S> entityClass,
CursorPreparer preparer,
org.springframework.data.mongodb.core.MongoTemplate.DocumentCallback<T> objectCallback) |
protected <T> List<T> |
doFind(String collectionName,
org.bson.Document query,
org.bson.Document fields,
Class<T> entityClass)
Map the results of an ad-hoc query on the default MongoDB collection to a List using the template's converter.
|
protected <T> List<T> |
doFind(String collectionName,
org.bson.Document query,
org.bson.Document fields,
Class<T> entityClass,
CursorPreparer preparer)
Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.
|
protected <T> List<T> |
doFindAndDelete(String collectionName,
Query query,
Class<T> entityClass)
Retrieve and remove all documents matching the given
query by calling find(Query, Class, String)
and remove(Query, Class, String), whereas the Query for remove(Query, Class, String) is
constructed out of the find result. |
protected <T> T |
doFindAndModify(String collectionName,
org.bson.Document query,
org.bson.Document fields,
org.bson.Document sort,
Class<T> entityClass,
UpdateDefinition update,
FindAndModifyOptions options) |
protected <T> T |
doFindAndRemove(String collectionName,
org.bson.Document query,
org.bson.Document fields,
org.bson.Document sort,
Collation collation,
Class<T> entityClass)
Map the results of an ad-hoc query on the default MongoDB collection to an object using the template's converter.
|
protected <T> T |
doFindAndReplace(String collectionName,
org.bson.Document mappedQuery,
org.bson.Document mappedFields,
org.bson.Document mappedSort,
com.mongodb.client.model.Collation collation,
Class<?> entityType,
org.bson.Document replacement,
FindAndReplaceOptions options,
Class<T> resultType)
Customize this part for findAndReplace.
|
protected <T> T |
doFindOne(String collectionName,
org.bson.Document query,
org.bson.Document fields,
Class<T> entityClass)
Map the results of an ad-hoc query on the default MongoDB collection to an object using the template's converter.
|
protected <T> T |
doFindOne(String collectionName,
org.bson.Document query,
org.bson.Document fields,
CursorPreparer preparer,
Class<T> entityClass)
Map the results of an ad-hoc query on the default MongoDB collection to an object using the template's converter.
|
protected com.mongodb.client.MongoDatabase |
doGetDatabase() |
protected <T> T |
doInsert(String collectionName,
T objectToSave,
MongoWriter<T> writer) |
protected <T> Collection<T> |
doInsertAll(Collection<? extends T> listToSave,
MongoWriter<T> writer) |
protected <T> Collection<T> |
doInsertBatch(String collectionName,
Collection<? extends T> batchToSave,
MongoWriter<T> writer) |
protected <T> com.mongodb.client.result.DeleteResult |
doRemove(String collectionName,
Query query,
Class<T> entityClass,
boolean multi) |
protected <T> T |
doSave(String collectionName,
T objectToSave,
MongoWriter<T> writer) |
protected <T> CloseableIterator<T> |
doStream(Query query,
Class<?> entityType,
String collectionName,
Class<T> returnType) |
protected com.mongodb.client.result.UpdateResult |
doUpdate(String collectionName,
Query query,
UpdateDefinition update,
Class<?> entityClass,
boolean upsert,
boolean multi) |
<T> void |
dropCollection(Class<T> entityClass)
Drop the collection with the name indicated by the entity class.
|
void |
dropCollection(String collectionName)
Drop the collection with the given name.
|
protected void |
ensureNotCollectionLike(Object source)
|
protected void |
ensureNotIterable(Object source)
Deprecated.
since 3.2. Call
ensureNotCollectionLike(Object) instead. |
long |
estimatedCount(String collectionName)
Estimate the number of documents in the given collection based on collection statistics.
|
<T> T |
execute(Class<?> entityClass,
CollectionCallback<T> callback)
Executes the given
CollectionCallback on the entity collection of the specified class. |
<T> T |
execute(DbCallback<T> action)
Executes a
DbCallback translating any exceptions as necessary. |
<T> T |
execute(String collectionName,
CollectionCallback<T> callback)
Executes the given
CollectionCallback on the collection of the given name. |
org.bson.Document |
executeCommand(org.bson.Document command)
Execute a MongoDB command.
|
org.bson.Document |
executeCommand(org.bson.Document command,
com.mongodb.ReadPreference readPreference)
Execute a MongoDB command.
|
org.bson.Document |
executeCommand(String jsonCommand)
Execute the a MongoDB command expressed as a JSON string.
|
void |
executeQuery(Query query,
String collectionName,
DocumentCallbackHandler dch)
Execute a MongoDB query and iterate over the query results on a per-document basis with a DocumentCallbackHandler.
|
protected void |
executeQuery(Query query,
String collectionName,
DocumentCallbackHandler documentCallbackHandler,
CursorPreparer preparer)
Execute a MongoDB query and iterate over the query results on a per-document basis with a
DocumentCallbackHandler using the provided CursorPreparer. |
boolean |
exists(Query query,
Class<?> entityClass)
Determine result of given
Query contains at least one element. |
boolean |
exists(Query query,
Class<?> entityClass,
String collectionName)
Determine result of given
Query contains at least one element. |
boolean |
exists(Query query,
String collectionName)
Determine result of given
Query contains at least one element. |
<T> List<T> |
find(Query query,
Class<T> entityClass)
Map the results of an ad-hoc query on the collection for the entity class to a List of the specified type.
|
<T> List<T> |
find(Query query,
Class<T> entityClass,
String collectionName)
Map the results of an ad-hoc query on the specified collection to a List of the specified type.
|
<T> List<T> |
findAll(Class<T> entityClass)
Query for a list of objects of type T from the collection used by the entity class.
|
<T> List<T> |
findAll(Class<T> entityClass,
String collectionName)
Query for a list of objects of type T from the specified collection.
|
<T> List<T> |
findAllAndRemove(Query query,
Class<T> entityClass)
Returns and removes all documents matching the given query form the collection used to store the entityClass.
|
<T> List<T> |
findAllAndRemove(Query query,
Class<T> entityClass,
String collectionName)
Returns and removes all documents that match the provided query document criteria from the the collection used to
store the entityClass.
|
<T> List<T> |
findAllAndRemove(Query query,
String collectionName)
Returns and removes all documents form the specified collection that match the provided query.
|
<T> T |
findAndModify(Query query,
UpdateDefinition update,
Class<T> entityClass)
|
<T> T |
findAndModify(Query query,
UpdateDefinition update,
Class<T> entityClass,
String collectionName)
|
<T> T |
findAndModify(Query query,
UpdateDefinition update,
FindAndModifyOptions options,
Class<T> entityClass)
Triggers findAndModify
to apply provided
Update on documents matching Criteria of given Query taking
FindAndModifyOptions into account. |
<T> T |
findAndModify(Query query,
UpdateDefinition update,
FindAndModifyOptions options,
Class<T> entityClass,
String collectionName)
Triggers findAndModify
to apply provided
Update on documents matching Criteria of given Query taking
FindAndModifyOptions into account. |
<T> T |
findAndRemove(Query query,
Class<T> entityClass)
Map the results of an ad-hoc query on the collection for the entity type to a single instance of an object of the
specified type.
|
<T> T |
findAndRemove(Query query,
Class<T> entityClass,
String collectionName)
Map the results of an ad-hoc query on the specified collection to a single instance of an object of the specified
type.
|
<S,T> T |
findAndReplace(Query query,
S replacement,
FindAndReplaceOptions options,
Class<S> entityType,
String collectionName,
Class<T> resultType)
Triggers
findOneAndReplace
to replace a single document matching
Criteria of given Query with the replacement document
taking FindAndReplaceOptions into account. |
<T> T |
findById(Object id,
Class<T> entityClass)
Returns a document with the given id mapped onto the given class.
|
<T> T |
findById(Object id,
Class<T> entityClass,
String collectionName)
Returns the document with the given id from the given collection mapped onto the given target class.
|
<T> List<T> |
findDistinct(Query query,
String field,
Class<?> entityClass,
Class<T> resultClass)
Finds the distinct values for a specified field across a single
MongoCollection or view and
returns the results in a List. |
<T> List<T> |
findDistinct(Query query,
String field,
String collectionName,
Class<?> entityClass,
Class<T> resultClass)
Finds the distinct values for a specified field across a single
MongoCollection or view and
returns the results in a List. |
<T> T |
findOne(Query query,
Class<T> entityClass)
Map the results of an ad-hoc query on the collection for the entity class to a single instance of an object of the
specified type.
|
<T> T |
findOne(Query query,
Class<T> entityClass,
String collectionName)
Map the results of an ad-hoc query on the specified collection to a single instance of an object of the specified
type.
|
<T> GeoResults<T> |
geoNear(NearQuery near,
Class<?> domainType,
String collectionName,
Class<T> returnType) |
<T> GeoResults<T> |
geoNear(NearQuery near,
Class<T> entityClass)
Returns
GeoResults for all entities matching the given NearQuery. |
<T> GeoResults<T> |
geoNear(NearQuery near,
Class<T> domainType,
String collectionName)
Returns
GeoResults for all entities matching the given NearQuery. |
com.mongodb.client.MongoCollection<org.bson.Document> |
getCollection(String collectionName)
Get a
MongoCollection by its name. |
String |
getCollectionName(Class<?> entityClass)
The collection name used for the specified class by this template.
|
Set<String> |
getCollectionNames()
A set of collection names.
|
MongoConverter |
getConverter()
Returns the default
MongoConverter. |
com.mongodb.client.MongoDatabase |
getDb() |
PersistenceExceptionTranslator |
getExceptionTranslator() |
MongoDatabaseFactory |
getMongoDatabaseFactory() |
MongoDatabaseFactory |
getMongoDbFactory()
Deprecated.
since 3.1.4. Use
getMongoDatabaseFactory() instead. |
<T> GroupByResults<T> |
group(Criteria criteria,
String inputCollectionName,
GroupBy groupBy,
Class<T> entityClass)
Execute a group operation restricting the rows to those which match the provided Criteria.
|
<T> GroupByResults<T> |
group(String inputCollectionName,
GroupBy groupBy,
Class<T> entityClass)
Execute a group operation over the entire collection.
|
IndexOperations |
indexOps(Class<?> entityClass)
Returns the operations that can be performed on indexes
|
IndexOperations |
indexOps(String collectionName)
Returns the operations that can be performed on indexes
|
IndexOperations |
indexOps(String collectionName,
Class<?> type)
Returns the operations that can be performed on indexes.
|
<T> ExecutableInsertOperation.ExecutableInsert<T> |
insert(Class<T> domainType)
Start creating an insert operation for given domainType.
|
<T> Collection<T> |
insert(Collection<? extends T> batchToSave,
Class<?> entityClass)
Insert a Collection of objects into a collection in a single batch write to the database.
|
<T> Collection<T> |
insert(Collection<? extends T> batchToSave,
String collectionName)
Insert a batch of objects into the specified collection in a single batch write to the database.
|
<T> T |
insert(T objectToSave)
Insert the object into the collection for the entity type of the object to save.
|
<T> T |
insert(T objectToSave,
String collectionName)
Insert the object into the specified collection.
|
<T> Collection<T> |
insertAll(Collection<? extends T> objectsToSave)
Insert a mixed Collection of objects into a database collection determining the collection name to use based on the
class.
|
protected Object |
insertDocument(String collectionName,
org.bson.Document document,
Class<?> entityClass) |
protected List<Object> |
insertDocumentList(String collectionName,
List<org.bson.Document> documents) |
<T> ExecutableMapReduceOperation.ExecutableMapReduce<T> |
mapReduce(Class<T> domainType)
Start creating a mapReduce operation for the given domainType.
|
<T> List<T> |
mapReduce(Query query,
Class<?> domainType,
String inputCollectionName,
String mapFunction,
String reduceFunction,
MapReduceOptions mapReduceOptions,
Class<T> resultType) |
<T> MapReduceResults<T> |
mapReduce(Query query,
String inputCollectionName,
String mapFunction,
String reduceFunction,
Class<T> entityClass)
Execute a map-reduce operation that takes a query.
|
<T> MapReduceResults<T> |
mapReduce(Query query,
String inputCollectionName,
String mapFunction,
String reduceFunction,
MapReduceOptions mapReduceOptions,
Class<T> entityClass)
Execute a map-reduce operation that takes a query and additional map-reduce options
|
<T> MapReduceResults<T> |
mapReduce(String inputCollectionName,
String mapFunction,
String reduceFunction,
Class<T> entityClass)
Execute a map-reduce operation.
|
<T> MapReduceResults<T> |
mapReduce(String inputCollectionName,
String mapFunction,
String reduceFunction,
MapReduceOptions mapReduceOptions,
Class<T> entityClass)
Execute a map-reduce operation that takes additional map-reduce options.
|
protected <T> T |
maybeCallAfterConvert(T object,
org.bson.Document document,
String collection) |
protected <T> T |
maybeCallAfterSave(T object,
org.bson.Document document,
String collection) |
protected <T> T |
maybeCallBeforeConvert(T object,
String collection) |
protected <T> T |
maybeCallBeforeSave(T object,
org.bson.Document document,
String collection) |
protected <E extends MongoMappingEvent<T>,T> |
maybeEmitEvent(E event) |
protected <T> T |
populateIdIfNecessary(T savedObject,
Object id)
Populates the id property of the saved object, if it's not set already.
|
protected com.mongodb.client.MongoCollection<org.bson.Document> |
prepareCollection(com.mongodb.client.MongoCollection<org.bson.Document> collection)
Prepare the collection before any processing is done using it.
|
protected com.mongodb.client.MongoDatabase |
prepareDatabase(com.mongodb.client.MongoDatabase database) |
protected com.mongodb.WriteConcern |
prepareWriteConcern(MongoAction mongoAction)
Prepare the WriteConcern before any processing is done using it.
|
<T> ExecutableFindOperation.ExecutableFind<T> |
query(Class<T> domainType)
Start creating a find operation for the given domainType.
|
<T> ExecutableRemoveOperation.ExecutableRemove<T> |
remove(Class<T> domainType)
Start creating a remove operation for the given domainType.
|
com.mongodb.client.result.DeleteResult |
remove(Object object)
Remove the given object from the collection by id and (if applicable) its
Version. |
com.mongodb.client.result.DeleteResult |
remove(Object object,
String collectionName)
Removes the given object from the given collection by id and (if applicable) its
Version. |
com.mongodb.client.result.DeleteResult |
remove(Query query,
Class<?> entityClass)
Remove all documents that match the provided query document criteria from the the collection used to store the
entityClass.
|
com.mongodb.client.result.DeleteResult |
remove(Query query,
Class<?> entityClass,
String collectionName)
Remove all documents that match the provided query document criteria from the the collection used to store the
entityClass.
|
com.mongodb.client.result.DeleteResult |
remove(Query query,
String collectionName)
Remove all documents from the specified collection that match the provided query document criteria.
|
protected String |
replaceWithResourceIfNecessary(String function) |
<T> T |
save(T objectToSave)
Save the object to the collection for the entity type of the object to save.
|
<T> T |
save(T objectToSave,
String collectionName)
Save the object to the specified collection.
|
protected Object |
saveDocument(String collectionName,
org.bson.Document dbDoc,
Class<?> entityClass) |
ScriptOperations |
scriptOps()
Returns the
ScriptOperations that can be performed on MongoDatabase level. |
void |
setApplicationContext(ApplicationContext applicationContext) |
void |
setEntityCallbacks(EntityCallbacks entityCallbacks)
|
void |
setReadPreference(com.mongodb.ReadPreference readPreference)
Used by @{link
prepareCollection(MongoCollection) to set the ReadPreference before any operations
are performed. |
void |
setSessionSynchronization(SessionSynchronization sessionSynchronization)
Define if
MongoTemplate should participate in transactions. |
void |
setWriteConcern(com.mongodb.WriteConcern writeConcern)
Configures the
WriteConcern to be used with the template. |
void |
setWriteConcernResolver(WriteConcernResolver writeConcernResolver)
Configures the
WriteConcernResolver to be used with the template. |
void |
setWriteResultChecking(WriteResultChecking resultChecking)
Configures the
WriteResultChecking to be used with the template. |
<T> CloseableIterator<T> |
stream(Query query,
Class<T> entityType)
Executes the given
Query on the entity collection of the specified entityType backed by a Mongo DB
FindIterable. |
<T> CloseableIterator<T> |
stream(Query query,
Class<T> entityType,
String collectionName)
Executes the given
Query on the entity collection of the specified entityType and collection backed
by a Mongo DB FindIterable. |
<T> ExecutableUpdateOperation.ExecutableUpdate<T> |
update(Class<T> domainType)
Start creating an update operation for the given domainType.
|
com.mongodb.client.result.UpdateResult |
updateFirst(Query query,
UpdateDefinition update,
Class<?> entityClass)
Updates the first object that is found in the collection of the entity class that matches the query document with
the provided update document.
|
com.mongodb.client.result.UpdateResult |
updateFirst(Query query,
UpdateDefinition update,
Class<?> entityClass,
String collectionName)
Updates the first object that is found in the specified collection that matches the query document criteria with
the provided updated document.
|
com.mongodb.client.result.UpdateResult |
updateFirst(Query query,
UpdateDefinition update,
String collectionName)
Updates the first object that is found in the specified collection that matches the query document criteria with
the provided updated document.
|
com.mongodb.client.result.UpdateResult |
updateMulti(Query query,
UpdateDefinition update,
Class<?> entityClass)
Updates all objects that are found in the collection for the entity class that matches the query document criteria
with the provided updated document.
|
com.mongodb.client.result.UpdateResult |
updateMulti(Query query,
UpdateDefinition update,
Class<?> entityClass,
String collectionName)
Updates all objects that are found in the collection for the entity class that matches the query document criteria
with the provided updated document.
|
com.mongodb.client.result.UpdateResult |
updateMulti(Query query,
UpdateDefinition update,
String collectionName)
Updates all objects that are found in the specified collection that matches the query document criteria with the
provided updated document.
|
com.mongodb.client.result.UpdateResult |
upsert(Query query,
UpdateDefinition update,
Class<?> entityClass)
Performs an upsert.
|
com.mongodb.client.result.UpdateResult |
upsert(Query query,
UpdateDefinition update,
Class<?> entityClass,
String collectionName)
Performs an upsert.
|
com.mongodb.client.result.UpdateResult |
upsert(Query query,
UpdateDefinition update,
String collectionName)
Performs an upsert.
|
MongoTemplate |
withSession(com.mongodb.client.ClientSession session)
Obtain a
ClientSession bound instance of MongoOperations. |
SessionScoped |
withSession(com.mongodb.ClientSessionOptions options)
Obtain a
session bound instance of SessionScoped binding a new ClientSession
with given sessionOptions to each and every command issued against MongoDB. |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitestimatedCount, findAndReplace, findAndReplace, findAndReplace, findAndReplace, findAndReplace, findAndReplace, findDistinct, findDistinct, withSessionpublic MongoTemplate(com.mongodb.client.MongoClient mongoClient,
String databaseName)
mongoClient - must not be null.databaseName - must not be null or empty.public MongoTemplate(MongoDatabaseFactory mongoDbFactory)
mongoDbFactory - must not be null.public MongoTemplate(MongoDatabaseFactory mongoDbFactory, @Nullable MongoConverter mongoConverter)
mongoDbFactory - must not be null.mongoConverter - public void setWriteResultChecking(@Nullable WriteResultChecking resultChecking)
WriteResultChecking to be used with the template. Setting null will reset the
default of DEFAULT_WRITE_RESULT_CHECKING.resultChecking - public void setWriteConcern(@Nullable com.mongodb.WriteConcern writeConcern)
WriteConcern to be used with the template. If none is configured the WriteConcern
configured on the MongoDatabaseFactory will apply.writeConcern - public void setWriteConcernResolver(@Nullable WriteConcernResolver writeConcernResolver)
WriteConcernResolver to be used with the template.writeConcernResolver - public void setReadPreference(@Nullable com.mongodb.ReadPreference readPreference)
prepareCollection(MongoCollection) to set the ReadPreference before any operations
are performed.readPreference - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
setApplicationContext in interface ApplicationContextAwareBeansExceptionpublic void setEntityCallbacks(EntityCallbacks entityCallbacks)
EntityCallbacks instance to use when invoking
callbacks like the BeforeSaveCallback.
EntityCallbacks.entityCallbacks - must not be null.IllegalArgumentException - if the given instance is null.public MongoConverter getConverter()
MongoConverter.getConverter in interface MongoOperationspublic <T> CloseableIterator<T> stream(Query query, Class<T> entityType)
MongoOperationsQuery on the entity collection of the specified entityType backed by a Mongo DB
FindIterable.
Returns a CloseableIterator that wraps the a Mongo DB FindIterable that needs to
be closed.
stream in interface MongoOperationsT - element return typequery - the query class that specifies the criteria used to find a record and also an optional fields
specification. Must not be null.entityType - must not be null.public <T> CloseableIterator<T> stream(Query query, Class<T> entityType, String collectionName)
MongoOperationsQuery on the entity collection of the specified entityType and collection backed
by a Mongo DB FindIterable.
Returns a CloseableIterator that wraps the a Mongo DB FindIterable that needs to
be closed.
stream in interface MongoOperationsT - element return typequery - the query class that specifies the criteria used to find a record and also an optional fields
specification. Must not be null.entityType - must not be null.collectionName - must not be null or empty.protected <T> CloseableIterator<T> doStream(Query query, Class<?> entityType, String collectionName, Class<T> returnType)
public String getCollectionName(Class<?> entityClass)
MongoOperationsgetCollectionName in interface MongoOperationsentityClass - must not be null.public org.bson.Document executeCommand(String jsonCommand)
MongoOperationsDocument.parse(String) to
obtain the Document holding the actual command. Any errors that result from executing this command will be
converted into Spring's DAO exception hierarchy.executeCommand in interface MongoOperationsjsonCommand - a MongoDB command expressed as a JSON string. Must not be null.public org.bson.Document executeCommand(org.bson.Document command)
MongoOperationsexecuteCommand in interface MongoOperationscommand - a MongoDB command.public org.bson.Document executeCommand(org.bson.Document command,
@Nullable
com.mongodb.ReadPreference readPreference)
MongoOperationsexecuteCommand in interface MongoOperationscommand - a MongoDB command, must not be null.readPreference - read preferences to use, can be null.public void executeQuery(Query query, String collectionName, DocumentCallbackHandler dch)
MongoOperationsexecuteQuery in interface MongoOperationsquery - the query class that specifies the criteria used to find a record and also an optional fields
specification. Must not be null.collectionName - name of the collection to retrieve the objects from.dch - the handler that will extract results, one document at a time.protected void executeQuery(Query query, String collectionName, DocumentCallbackHandler documentCallbackHandler, @Nullable CursorPreparer preparer)
DocumentCallbackHandler using the provided CursorPreparer.query - the query class that specifies the criteria used to find a record and also an optional fields
specification, must not be null.collectionName - name of the collection to retrieve the objects fromdocumentCallbackHandler - the handler that will extract results, one document at a timepreparer - allows for customization of the FindIterable used when iterating over the result set,
(apply limits, skips and so on).public <T> T execute(DbCallback<T> action)
MongoOperationsDbCallback translating any exceptions as necessary.
execute in interface MongoOperationsT - return type.action - callback object that specifies the MongoDB actions to perform on the passed in DB instance. Must not
be null.public <T> T execute(Class<?> entityClass, CollectionCallback<T> callback)
MongoOperationsCollectionCallback on the entity collection of the specified class.
execute in interface MongoOperationsT - return type.entityClass - class that determines the collection to use. Must not be null.callback - callback object that specifies the MongoDB action. Must not be null.public <T> T execute(String collectionName, CollectionCallback<T> callback)
MongoOperationsCollectionCallback on the collection of the given name.
execute in interface MongoOperationsT - return type.collectionName - the name of the collection that specifies which MongoCollection instance will be
passed into. Must not be null or empty.callback - callback object that specifies the MongoDB action the callback action. Must not be null.public SessionScoped withSession(com.mongodb.ClientSessionOptions options)
MongoOperationssession bound instance of SessionScoped binding a new ClientSession
with given sessionOptions to each and every command issued against MongoDB.withSession in interface MongoOperationsoptions - must not be null.SessionScoped. Never null.public MongoTemplate withSession(com.mongodb.client.ClientSession session)
MongoOperationsClientSession bound instance of MongoOperations.
ClientSession lifecycle.withSession in interface MongoOperationssession - must not be null.ClientSession bound instance of MongoOperations.public void setSessionSynchronization(SessionSynchronization sessionSynchronization)
MongoTemplate should participate in transactions. Default is set to
SessionSynchronization.ON_ACTUAL_TRANSACTION.public <T> com.mongodb.client.MongoCollection<org.bson.Document> createCollection(Class<T> entityClass)
MongoOperationscreateCollection in interface MongoOperationsentityClass - class that determines the collection to create.public <T> com.mongodb.client.MongoCollection<org.bson.Document> createCollection(Class<T> entityClass, @Nullable CollectionOptions collectionOptions)
MongoOperationscreateCollection in interface MongoOperationsentityClass - class that determines the collection to create. Must not be null.collectionOptions - options to use when creating the collection.public com.mongodb.client.MongoCollection<org.bson.Document> createCollection(String collectionName)
MongoOperationscreateCollection in interface MongoOperationscollectionName - name of the collection.public com.mongodb.client.MongoCollection<org.bson.Document> createCollection(String collectionName, @Nullable CollectionOptions collectionOptions)
MongoOperationscreateCollection in interface MongoOperationscollectionName - name of the collection. Must not be null nor empty.collectionOptions - options to use when creating the collection.public com.mongodb.client.MongoCollection<org.bson.Document> getCollection(String collectionName)
MongoOperationsMongoCollection by its name. The returned collection may not exists yet (except in local memory) and
is created on first interaction with the server. Collections can be explicitly created via
MongoOperations.createCollection(Class). Please make sure to check if the collection exists first.
getCollection in interface MongoOperationscollectionName - name of the collection. Must not be null.public <T> boolean collectionExists(Class<T> entityClass)
MongoOperationscollectionExists in interface MongoOperationsentityClass - class that determines the name of the collection. Must not be null.public boolean collectionExists(String collectionName)
MongoOperationscollectionExists in interface MongoOperationscollectionName - name of the collection. Must not be null.public <T> void dropCollection(Class<T> entityClass)
MongoOperationsdropCollection in interface MongoOperationsentityClass - class that determines the collection to drop/delete. Must not be null.public void dropCollection(String collectionName)
MongoOperationsdropCollection in interface MongoOperationscollectionName - name of the collection to drop/delete.public IndexOperations indexOps(String collectionName)
MongoOperationsindexOps in interface IndexOperationsProviderindexOps in interface MongoOperationscollectionName - name of the MongoDB collection, must not be null.public IndexOperations indexOps(String collectionName, @Nullable Class<?> type)
IndexOperationsProviderindexOps in interface IndexOperationsProvidercollectionName - name of the MongoDB collection, must not be null.type - the type used for field mapping. Can be null.public IndexOperations indexOps(Class<?> entityClass)
MongoOperationsindexOps in interface MongoOperationspublic BulkOperations bulkOps(BulkOperations.BulkMode bulkMode, String collectionName)
MongoOperationsBulkOperations for the given collection. #bulkOps(BulkMode, Class, String) to get full type specific support.bulkOps in interface MongoOperationsbulkMode - the BulkOperations.BulkMode to use for bulk operations, must not be null.collectionName - the name of the collection to work on, must not be null or empty.BulkOperations on the named collectionpublic BulkOperations bulkOps(BulkOperations.BulkMode bulkMode, Class<?> entityClass)
MongoOperationsBulkOperations for the given entity type.bulkOps in interface MongoOperationsbulkMode - the BulkOperations.BulkMode to use for bulk operations, must not be null.entityClass - the name of the entity class, must not be null.BulkOperations on the named collection associated of the given entity class.public BulkOperations bulkOps(BulkOperations.BulkMode mode, @Nullable Class<?> entityType, String collectionName)
MongoOperationsBulkOperations for the given entity type and collection name.bulkOps in interface MongoOperationsmode - the BulkOperations.BulkMode to use for bulk operations, must not be null.entityType - the name of the entity class. Can be null.collectionName - the name of the collection to work on, must not be null or empty.BulkOperations on the named collection associated with the given entity class.public ScriptOperations scriptOps()
MongoOperationsScriptOperations that can be performed on MongoDatabase level.scriptOps in interface MongoOperations@Nullable public <T> T findOne(Query query, Class<T> entityClass)
MongoOperationsMongoConverter. Unless
configured otherwise, an instance of MappingMongoConverter will be used.
Query which can be created either using the BasicQuery or the more
feature rich Query.findOne in interface MongoOperationsquery - the query class that specifies the criteria used to find a record and also an optional fields
specification.entityClass - the parametrized type of the returned list.@Nullable public <T> T findOne(Query query, Class<T> entityClass, String collectionName)
MongoOperationsMongoConverter. Unless
configured otherwise, an instance of MappingMongoConverter will be used.
Query which can be created either using the BasicQuery or the more
feature rich Query.findOne in interface MongoOperationsquery - the query class that specifies the criteria used to find a record and also an optional fields
specification.entityClass - the parametrized type of the returned list.collectionName - name of the collection to retrieve the objects from.public boolean exists(Query query, Class<?> entityClass)
MongoOperationsQuery contains at least one element.exists in interface MongoOperationsquery - the Query class that specifies the criteria used to find a record.entityClass - the parametrized type.public boolean exists(Query query, String collectionName)
MongoOperationsQuery contains at least one element. MongoOperations.exists(Query, Class, String) to get full type specific support.exists in interface MongoOperationsquery - the Query class that specifies the criteria used to find a record.collectionName - name of the collection to check for objects.public boolean exists(Query query, @Nullable Class<?> entityClass, String collectionName)
MongoOperationsQuery contains at least one element.exists in interface MongoOperationsquery - the Query class that specifies the criteria used to find a record.entityClass - the parametrized type. Can be null.collectionName - name of the collection to check for objects.public <T> List<T> find(Query query, Class<T> entityClass)
MongoOperationsMongoConverter. Unless
configured otherwise, an instance of MappingMongoConverter will be used.
Query which can be created either using the BasicQuery or the more
feature rich Query.find in interface MongoOperationsquery - the query class that specifies the criteria used to find a record and also an optional fields
specification. Must not be null.entityClass - the parametrized type of the returned list. Must not be null.public <T> List<T> find(Query query, Class<T> entityClass, String collectionName)
MongoOperationsMongoConverter. Unless
configured otherwise, an instance of MappingMongoConverter will be used.
Query which can be created either using the BasicQuery or the more
feature rich Query.find in interface MongoOperationsquery - the query class that specifies the criteria used to find a record and also an optional fields
specification. Must not be null.entityClass - the parametrized type of the returned list. Must not be null.collectionName - name of the collection to retrieve the objects from. Must not be null.@Nullable public <T> T findById(Object id, Class<T> entityClass)
MongoOperationsfindById in interface MongoOperationsid - the id of the document to return. Must not be null.entityClass - the type the document shall be converted into. Must not be null.@Nullable public <T> T findById(Object id, Class<T> entityClass, String collectionName)
MongoOperationsfindById in interface MongoOperationsid - the id of the document to return.entityClass - the type to convert the document to.collectionName - the collection to query for the document.public <T> List<T> findDistinct(Query query, String field, Class<?> entityClass, Class<T> resultClass)
MongoOperationsMongoCollection or view and
returns the results in a List.findDistinct in interface MongoOperationsquery - filter Query to restrict search. Must not be null.field - the name of the field to inspect for distinct values. Must not be null.entityClass - the domain type used for determining the actual MongoCollection and mapping the
Query to the domain type fields. Must not be null.resultClass - the result type. Must not be null.public <T> List<T> findDistinct(Query query, String field, String collectionName, Class<?> entityClass, Class<T> resultClass)
MongoOperationsMongoCollection or view and
returns the results in a List.findDistinct in interface MongoOperationsquery - filter Query to restrict search. Must not be null.field - the name of the field to inspect for distinct values. Must not be null.collectionName - the explicit name of the actual MongoCollection. Must not be null.entityClass - the domain type used for mapping the Query to the domain type fields.resultClass - the result type. Must not be null.public <T> GeoResults<T> geoNear(NearQuery near, Class<T> entityClass)
MongoOperationsGeoResults for all entities matching the given NearQuery. Will consider entity mapping
information to determine the collection the query is ran against. Note, that MongoDB limits the number of results
by default. Make sure to add an explicit limit to the NearQuery if you expect a particular number of
results.
MongoDB 4.2 has removed the geoNear command. This method uses since version 2.2 aggregations and the
$geoNear aggregation command to emulate geoNear command functionality. We recommend using
aggregations directly:
TypedAggregation<T> geoNear = TypedAggregation.newAggregation(entityClass, Aggregation.geoNear(near, "dis"))
.withOptions(AggregationOptions.builder().collation(near.getCollation()).build());
AggregationResults<Document> results = aggregate(geoNear, Document.class);
geoNear in interface MongoOperationsnear - must not be null.entityClass - must not be null.public <T> GeoResults<T> geoNear(NearQuery near, Class<T> domainType, String collectionName)
MongoOperationsGeoResults for all entities matching the given NearQuery. Note, that MongoDB limits the
number of results by default. Make sure to add an explicit limit to the NearQuery if you expect a
particular number of results.
MongoDB 4.2 has removed the geoNear command. This method uses since version 2.2 aggregations and the
$geoNear aggregation command to emulate geoNear command functionality. We recommend using
aggregations directly:
TypedAggregation<T> geoNear = TypedAggregation.newAggregation(entityClass, Aggregation.geoNear(near, "dis"))
.withOptions(AggregationOptions.builder().collation(near.getCollation()).build());
AggregationResults<Document> results = aggregate(geoNear, Document.class);
geoNear in interface MongoOperationsnear - must not be null.domainType - must not be null.collectionName - the collection to trigger the query against. If no collection name is given the entity class
will be inspected. Must not be null nor empty.public <T> GeoResults<T> geoNear(NearQuery near, Class<?> domainType, String collectionName, Class<T> returnType)
@Nullable public <T> T findAndModify(Query query, UpdateDefinition update, Class<T> entityClass)
MongoOperationsfindAndModify in interface MongoOperationsquery - the Query class that specifies the Criteria used to find a record and also an optional
fields specification. Must not be null.update - the UpdateDefinition to apply on matching documents. Must not be null.entityClass - the parametrized type. Must not be null.Update,
AggregationUpdate@Nullable public <T> T findAndModify(Query query, UpdateDefinition update, Class<T> entityClass, String collectionName)
MongoOperationsfindAndModify in interface MongoOperationsquery - the Query class that specifies the Criteria used to find a record and also an optional
fields specification. Must not be null.update - the UpdateDefinition to apply on matching documents. Must not be null.entityClass - the parametrized type. Must not be null.collectionName - the collection to query. Must not be null.Update,
AggregationUpdate@Nullable public <T> T findAndModify(Query query, UpdateDefinition update, FindAndModifyOptions options, Class<T> entityClass)
MongoOperationsUpdate on documents matching Criteria of given Query taking
FindAndModifyOptions into account.findAndModify in interface MongoOperationsquery - the Query class that specifies the Criteria used to find a record and also an optional
fields specification.update - the UpdateDefinition to apply on matching documents.options - the FindAndModifyOptions holding additional information.entityClass - the parametrized type.FindAndModifyOptions.isReturnNew() this will either be the object as it was before the update or as
it is after the update.Update,
AggregationUpdate@Nullable public <T> T findAndModify(Query query, UpdateDefinition update, FindAndModifyOptions options, Class<T> entityClass, String collectionName)
MongoOperationsUpdate on documents matching Criteria of given Query taking
FindAndModifyOptions into account.findAndModify in interface MongoOperationsquery - the Query class that specifies the Criteria used to find a record and also an optional
fields specification. Must not be null.update - the UpdateDefinition to apply on matching documents. Must not be null.options - the FindAndModifyOptions holding additional information. Must not be null.entityClass - the parametrized type. Must not be null.collectionName - the collection to query. Must not be null.FindAndModifyOptions.isReturnNew() this will either be the object as it was before the update or as
it is after the update.Update,
AggregationUpdatepublic <S,T> T findAndReplace(Query query, S replacement, FindAndReplaceOptions options, Class<S> entityType, String collectionName, Class<T> resultType)
MongoOperationsCriteria of given Query with the replacement document
taking FindAndReplaceOptions into account.findAndReplace in interface MongoOperationsquery - the Query class that specifies the Criteria used to find a record and also an optional
fields specification. Must not be null.replacement - the replacement document. Must not be null.options - the FindAndModifyOptions holding additional information. Must not be null.entityType - the type used for mapping the Query to domain type fields. Must not be null.collectionName - the collection to query. Must not be null.resultType - the parametrized type projection return type. Must not be null, use the domain type of
Object.class instead.FindAndReplaceOptions.isReturnNew() this will either be the object as it was before the update or
as it is after the update.@Nullable public <T> T findAndRemove(Query query, Class<T> entityClass)
MongoOperationsMongoConverter.
Query which can be created either using the BasicQuery or the more
feature rich Query.findAndRemove in interface MongoOperationsquery - the query class that specifies the criteria used to find a record and also an optional fields
specification.entityClass - the parametrized type of the returned list.@Nullable public <T> T findAndRemove(Query query, Class<T> entityClass, String collectionName)
MongoOperationsMongoConverter. Unless
configured otherwise, an instance of MappingMongoConverter will be used.
Query which can be created either using the BasicQuery or the more
feature rich Query.findAndRemove in interface MongoOperationsquery - the query class that specifies the criteria used to find a record and also an optional fields
specification.entityClass - the parametrized type of the returned list.collectionName - name of the collection to retrieve the objects from.public long count(Query query, Class<?> entityClass)
MongoOperationsQuery by querying the collection of the given entity class.
offset and limit can have direct
influence on the resulting number of documents found as those values are passed on to the server and potentially
limit the range and order within which the server performs the count operation. Use an unpaged query to
count all matches.
aggregation execution even for empty queries which may have an impact on performance, but guarantees
shard, session and transaction compliance. In case an inaccurate count satisfies the applications needs use
MongoOperations.estimatedCount(Class) for empty queries instead.count in interface MongoOperationsquery - the Query class that specifies the criteria used to find documents. Must not be
null.entityClass - class that determines the collection to use. Must not be null.public long count(Query query, String collectionName)
MongoOperationsQuery querying the given collection. The given Query
must solely consist of document field references as we lack type information to map potential property references
onto document fields. Use MongoOperations.count(Query, Class, String) to get full type specific support. offset and limit can have direct
influence on the resulting number of documents found as those values are passed on to the server and potentially
limit the range and order within which the server performs the count operation. Use an unpaged query to
count all matches.
aggregation execution even for empty queries which may have an impact on performance, but guarantees
shard, session and transaction compliance. In case an inaccurate count satisfies the applications needs use
MongoOperations.estimatedCount(String) for empty queries instead.count in interface MongoOperationsquery - the Query class that specifies the criteria used to find documents.collectionName - must not be null or empty.MongoOperations.count(Query, Class, String)public long count(Query query, @Nullable Class<?> entityClass, String collectionName)
MongoOperationsQuery by querying the given collection using the given entity
class to map the given Query. offset and limit can have direct
influence on the resulting number of documents found as those values are passed on to the server and potentially
limit the range and order within which the server performs the count operation. Use an unpaged query to
count all matches.
aggregation execution even for empty queries which may have an impact on performance, but guarantees
shard, session and transaction compliance. In case an inaccurate count satisfies the applications needs use
MongoOperations.estimatedCount(String) for empty queries instead.count in interface MongoOperationsquery - the Query class that specifies the criteria used to find documents. Must not be
null.entityClass - the parametrized type. Can be null.collectionName - must not be null or empty.protected long doCount(String collectionName, org.bson.Document filter, com.mongodb.client.model.CountOptions options)
public long estimatedCount(String collectionName)
MongoOperationsestimatedCount in interface MongoOperationscollectionName - must not be null.protected long doEstimatedCount(String collectionName, com.mongodb.client.model.EstimatedDocumentCountOptions options)
public <T> T insert(T objectToSave)
MongoOperationsMongoConverter.
objectToSave must not be collection-like.insert in interface MongoOperationsobjectToSave - the object to store in the collection. Must not be null.public <T> T insert(T objectToSave,
String collectionName)
MongoOperationsMongoConverter. Unless
configured otherwise, an instance of MappingMongoConverter will be used.
objectToSave must not be collection-like.insert in interface MongoOperationsobjectToSave - the object to store in the collection. Must not be null.collectionName - name of the collection to store the object in. Must not be null.protected void ensureNotIterable(@Nullable Object source)
ensureNotCollectionLike(Object) instead.source - can be null.protected void ensureNotCollectionLike(@Nullable Object source)
source - can be null.protected com.mongodb.client.MongoCollection<org.bson.Document> prepareCollection(com.mongodb.client.MongoCollection<org.bson.Document> collection)
collection - @Nullable protected com.mongodb.WriteConcern prepareWriteConcern(MongoAction mongoAction)
WriteConcern will be defaulted to
WriteConcern.ACKNOWLEDGED when WriteResultChecking is set to WriteResultChecking.EXCEPTION.mongoAction - any MongoAction already configured or nullprotected <T> T doInsert(String collectionName, T objectToSave, MongoWriter<T> writer)
public <T> Collection<T> insert(Collection<? extends T> batchToSave, Class<?> entityClass)
MongoOperationsinsert in interface MongoOperationsbatchToSave - the batch of objects to save. Must not be null.entityClass - class that determines the collection to use. Must not be null.public <T> Collection<T> insert(Collection<? extends T> batchToSave, String collectionName)
MongoOperationsinsert in interface MongoOperationsbatchToSave - the list of objects to save. Must not be null.collectionName - name of the collection to store the object in. Must not be null.public <T> Collection<T> insertAll(Collection<? extends T> objectsToSave)
MongoOperationsinsertAll in interface MongoOperationsobjectsToSave - the list of objects to save. Must not be null.protected <T> Collection<T> doInsertAll(Collection<? extends T> listToSave, MongoWriter<T> writer)
protected <T> Collection<T> doInsertBatch(String collectionName, Collection<? extends T> batchToSave, MongoWriter<T> writer)
public <T> T save(T objectToSave)
MongoOperationsMongoConverter. Unless
configured otherwise, an instance of MappingMongoConverter will be used.
objectToSave must not be collection-like.save in interface MongoOperationsobjectToSave - the object to store in the collection. Must not be null.public <T> T save(T objectToSave,
String collectionName)
MongoOperationsMongoConverter. Unless
configured otherwise, an instance of MappingMongoConverter will be used.
objectToSave must not be collection-like.save in interface MongoOperationsobjectToSave - the object to store in the collection. Must not be null.collectionName - name of the collection to store the object in. Must not be null.protected <T> T doSave(String collectionName, T objectToSave, MongoWriter<T> writer)
protected Object insertDocument(String collectionName, org.bson.Document document, Class<?> entityClass)
protected List<Object> insertDocumentList(String collectionName, List<org.bson.Document> documents)
protected Object saveDocument(String collectionName, org.bson.Document dbDoc, Class<?> entityClass)
public com.mongodb.client.result.UpdateResult upsert(Query query, UpdateDefinition update, Class<?> entityClass)
MongoOperationssorting is not supported by db.collection.updateOne.
Use MongoOperations.findAndModify(Query, UpdateDefinition, FindAndModifyOptions, Class, String) instead.upsert in interface MongoOperationsquery - the query document that specifies the criteria used to select a record to be upserted. Must not be
null.update - the UpdateDefinition that contains the updated object or $ operators to manipulate
the existing object. Must not be null.entityClass - class that determines the collection to use. Must not be null.UpdateResult which lets you access the results of the previous write.Update,
AggregationUpdatepublic com.mongodb.client.result.UpdateResult upsert(Query query, UpdateDefinition update, String collectionName)
MongoOperationsMongoOperations.upsert(Query, UpdateDefinition, Class, String) to get full type specific
support. sorting is not supported by db.collection.updateOne.
Use MongoOperations.findAndModify(Query, UpdateDefinition, FindAndModifyOptions, Class, String) instead.upsert in interface MongoOperationsquery - the query document that specifies the criteria used to select a record to be upserted. Must not be
null.update - the UpdateDefinition that contains the updated object or $ operators to manipulate
the existing object. Must not be null.collectionName - name of the collection to update the object in.UpdateResult which lets you access the results of the previous write.Update,
AggregationUpdatepublic com.mongodb.client.result.UpdateResult upsert(Query query, UpdateDefinition update, Class<?> entityClass, String collectionName)
MongoOperationsupsert in interface MongoOperationsquery - the query document that specifies the criteria used to select a record to be upserted. Must not be
null.update - the UpdateDefinition that contains the updated object or $ operators to manipulate
the existing object. Must not be null.entityClass - class of the pojo to be operated on. Must not be null.collectionName - name of the collection to update the object in. Must not be null.UpdateResult which lets you access the results of the previous write.Update,
AggregationUpdatepublic com.mongodb.client.result.UpdateResult updateFirst(Query query, UpdateDefinition update, Class<?> entityClass)
MongoOperationsupdateFirst in interface MongoOperationsquery - the query document that specifies the criteria used to select a record to be updated. Must not be
null.update - the UpdateDefinition that contains the updated object or $ operators to manipulate
the existing. Must not be null.entityClass - class that determines the collection to use.UpdateResult which lets you access the results of the previous write.Update,
AggregationUpdatepublic com.mongodb.client.result.UpdateResult updateFirst(Query query, UpdateDefinition update, String collectionName)
MongoOperationsMongoOperations.updateFirst(Query, UpdateDefinition, Class, String) to get full type specific
support. sorting is not supported by db.collection.updateOne.
Use MongoOperations.findAndModify(Query, UpdateDefinition, Class, String) instead.updateFirst in interface MongoOperationsquery - the query document that specifies the criteria used to select a record to be updated. Must not be
null.update - the UpdateDefinition that contains the updated object or $ operators to manipulate
the existing. Must not be null.collectionName - name of the collection to update the object in. Must not be null.UpdateResult which lets you access the results of the previous write.Update,
AggregationUpdatepublic com.mongodb.client.result.UpdateResult updateFirst(Query query, UpdateDefinition update, Class<?> entityClass, String collectionName)
MongoOperationsupdateFirst in interface MongoOperationsquery - the query document that specifies the criteria used to select a record to be updated. Must not be
null.update - the UpdateDefinition that contains the updated object or $ operators to manipulate
the existing. Must not be null.entityClass - class of the pojo to be operated on. Must not be null.collectionName - name of the collection to update the object in. Must not be null.UpdateResult which lets you access the results of the previous write.Update,
AggregationUpdatepublic com.mongodb.client.result.UpdateResult updateMulti(Query query, UpdateDefinition update, Class<?> entityClass)
MongoOperationsupdateMulti in interface MongoOperationsquery - the query document that specifies the criteria used to select a record to be updated. Must not be
null.update - the UpdateDefinition that contains the updated object or $ operators to manipulate
the existing. Must not be null.entityClass - class of the pojo to be operated on. Must not be null.UpdateResult which lets you access the results of the previous write.Update,
AggregationUpdatepublic com.mongodb.client.result.UpdateResult updateMulti(Query query, UpdateDefinition update, String collectionName)
MongoOperationsMongoOperations.updateMulti(Query, UpdateDefinition, Class, String) to get full type specific
support.updateMulti in interface MongoOperationsquery - the query document that specifies the criteria used to select a record to be updated. Must not be
null.update - the UpdateDefinition that contains the updated object or $ operators to manipulate
the existing. Must not be null.collectionName - name of the collection to update the object in. Must not be null.UpdateResult which lets you access the results of the previous write.Update,
AggregationUpdatepublic com.mongodb.client.result.UpdateResult updateMulti(Query query, UpdateDefinition update, Class<?> entityClass, String collectionName)
MongoOperationsupdateMulti in interface MongoOperationsquery - the query document that specifies the criteria used to select a record to be updated. Must not be
null.update - the UpdateDefinition that contains the updated object or $ operators to manipulate
the existing. Must not be null.entityClass - class of the pojo to be operated on. Must not be null.collectionName - name of the collection to update the object in. Must not be null.UpdateResult which lets you access the results of the previous write.Update,
AggregationUpdateprotected com.mongodb.client.result.UpdateResult doUpdate(String collectionName, Query query, UpdateDefinition update, @Nullable Class<?> entityClass, boolean upsert, boolean multi)
public com.mongodb.client.result.DeleteResult remove(Object object)
MongoOperationsVersion. DeleteResult.getDeletedCount() for insight whether an acknowledged remove operation was successful or not.remove in interface MongoOperationsobject - must not be null.DeleteResult which lets you access the results of the previous delete.public com.mongodb.client.result.DeleteResult remove(Object object, String collectionName)
MongoOperationsVersion. DeleteResult.getDeletedCount() for insight whether an acknowledged remove operation was successful or not.remove in interface MongoOperationsobject - must not be null.collectionName - name of the collection where the objects will removed, must not be null or empty.DeleteResult which lets you access the results of the previous delete.public com.mongodb.client.result.DeleteResult remove(Query query, String collectionName)
MongoOperationsMongoOperations.remove(Query, Class, String) to get full type specific support.remove in interface MongoOperationsquery - the query document that specifies the criteria used to remove a record.collectionName - name of the collection where the objects will removed, must not be null or empty.DeleteResult which lets you access the results of the previous delete.public com.mongodb.client.result.DeleteResult remove(Query query, Class<?> entityClass)
MongoOperationsremove in interface MongoOperationsquery - the query document that specifies the criteria used to remove a record.entityClass - class that determines the collection to use.DeleteResult which lets you access the results of the previous delete.public com.mongodb.client.result.DeleteResult remove(Query query, Class<?> entityClass, String collectionName)
MongoOperationsremove in interface MongoOperationsquery - the query document that specifies the criteria used to remove a record.entityClass - class of the pojo to be operated on. Can be null.collectionName - name of the collection where the objects will removed, must not be null or empty.DeleteResult which lets you access the results of the previous delete.protected <T> com.mongodb.client.result.DeleteResult doRemove(String collectionName, Query query, @Nullable Class<T> entityClass, boolean multi)
public <T> List<T> findAll(Class<T> entityClass)
MongoOperationsMongoConverter. Unless
configured otherwise, an instance of MappingMongoConverter will be used.
findAll in interface MongoOperationsentityClass - the parametrized type of the returned list.public <T> List<T> findAll(Class<T> entityClass, String collectionName)
MongoOperationsMongoConverter. Unless
configured otherwise, an instance of MappingMongoConverter will be used.
findAll in interface MongoOperationsentityClass - the parametrized type of the returned list.collectionName - name of the collection to retrieve the objects from.public <T> MapReduceResults<T> mapReduce(String inputCollectionName, String mapFunction, String reduceFunction, Class<T> entityClass)
MongoOperationsmapReduce in interface MongoOperationsinputCollectionName - the collection where the map-reduce will read from. Must not be null.mapFunction - The JavaScript map function.reduceFunction - The JavaScript reduce functionentityClass - The parametrized type of the returned list. Must not be null.public <T> MapReduceResults<T> mapReduce(String inputCollectionName, String mapFunction, String reduceFunction, @Nullable MapReduceOptions mapReduceOptions, Class<T> entityClass)
MongoOperationsmapReduce in interface MongoOperationsinputCollectionName - the collection where the map-reduce will read from. Must not be null.mapFunction - The JavaScript map functionreduceFunction - The JavaScript reduce functionmapReduceOptions - Options that specify detailed map-reduce behavior.entityClass - The parametrized type of the returned list. Must not be null.public <T> MapReduceResults<T> mapReduce(Query query, String inputCollectionName, String mapFunction, String reduceFunction, Class<T> entityClass)
MongoOperationsmapReduce in interface MongoOperationsquery - The query to use to select the data for the map phase. Must not be null.inputCollectionName - the collection where the map-reduce will read from. Must not be null.mapFunction - The JavaScript map functionreduceFunction - The JavaScript reduce functionentityClass - The parametrized type of the returned list. Must not be null.public <T> MapReduceResults<T> mapReduce(Query query, String inputCollectionName, String mapFunction, String reduceFunction, @Nullable MapReduceOptions mapReduceOptions, Class<T> entityClass)
MongoOperationsmapReduce in interface MongoOperationsquery - The query to use to select the data for the map phase. Must not be null.inputCollectionName - the collection where the map-reduce will read from. Must not be null.mapFunction - The JavaScript map functionreduceFunction - The JavaScript reduce functionmapReduceOptions - Options that specify detailed map-reduce behaviorentityClass - The parametrized type of the returned list. Must not be null.public <T> List<T> mapReduce(Query query, Class<?> domainType, String inputCollectionName, String mapFunction, String reduceFunction, @Nullable MapReduceOptions mapReduceOptions, Class<T> resultType)
query - domainType - inputCollectionName - mapFunction - reduceFunction - mapReduceOptions - resultType - public <T> GroupByResults<T> group(String inputCollectionName, GroupBy groupBy, Class<T> entityClass)
MongoOperationsgroup in interface MongoOperationsinputCollectionName - the collection where the group operation will read fromgroupBy - the conditions under which the group operation will be performed, e.g. keys, initial document,
reduce function.entityClass - The parametrized type of the returned listpublic <T> GroupByResults<T> group(@Nullable Criteria criteria, String inputCollectionName, GroupBy groupBy, Class<T> entityClass)
MongoOperationsgroup in interface MongoOperationscriteria - The criteria that restricts the row that are considered for grouping. If not specified all rows are
considered.inputCollectionName - the collection where the group operation will read fromgroupBy - the conditions under which the group operation will be performed, e.g. keys, initial document,
reduce function.entityClass - The parametrized type of the returned listpublic <O> AggregationResults<O> aggregate(TypedAggregation<?> aggregation, Class<O> outputType)
MongoOperationsaggregate in interface MongoOperationsaggregation - The TypedAggregation specification holding the aggregation operations, must not be
null.outputType - The parametrized type of the returned list, must not be null.public <O> AggregationResults<O> aggregate(TypedAggregation<?> aggregation, String inputCollectionName, Class<O> outputType)
MongoOperationsaggregate in interface MongoOperationsaggregation - The TypedAggregation specification holding the aggregation operations, must not be
null.inputCollectionName - The name of the input collection to use for the aggreation.outputType - The parametrized type of the returned list, must not be null.public <O> AggregationResults<O> aggregate(Aggregation aggregation, Class<?> inputType, Class<O> outputType)
MongoOperationsaggregate in interface MongoOperationsaggregation - The Aggregation specification holding the aggregation operations, must not be
null.inputType - the inputType where the aggregation operation will read from, must not be null or
empty.outputType - The parametrized type of the returned list, must not be null.public <O> AggregationResults<O> aggregate(Aggregation aggregation, String collectionName, Class<O> outputType)
MongoOperationsaggregate in interface MongoOperationsaggregation - The Aggregation specification holding the aggregation operations, must not be
null.collectionName - the collection where the aggregation operation will read from, must not be null or
empty.outputType - The parametrized type of the returned list, must not be null.public <O> CloseableIterator<O> aggregateStream(TypedAggregation<?> aggregation, String inputCollectionName, Class<O> outputType)
MongoOperationsAggregateIterable.
Returns a CloseableIterator that wraps the a Mongo DB 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 aggregation explain. Enabling
explanation mode will throw an IllegalArgumentException.
aggregateStream in interface MongoOperationsaggregation - The TypedAggregation specification holding the aggregation operations, must not be
null.inputCollectionName - The name of the input collection to use for the aggreation.outputType - The parametrized type of the returned list, must not be null.public <O> CloseableIterator<O> aggregateStream(TypedAggregation<?> aggregation, Class<O> outputType)
MongoOperationsAggregateIterable.
CloseableIterator that wraps the a Mongo DB 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 explain. Enabling
explanation mode will throw an IllegalArgumentException.aggregateStream in interface MongoOperationsaggregation - The TypedAggregation specification holding the aggregation operations, must not be
null.outputType - The parametrized type of the returned list, must not be null.public <O> CloseableIterator<O> aggregateStream(Aggregation aggregation, Class<?> inputType, Class<O> outputType)
MongoOperationsAggregateIterable.
CloseableIterator that wraps the a Mongo DB AggregateIterable that
needs to be closed. The raw results will be mapped to the given entity class.
aggregation explain. Enabling
explanation mode will throw an IllegalArgumentException.aggregateStream in interface MongoOperationsaggregation - The Aggregation specification holding the aggregation operations, must not be
null.inputType - the inputType where the aggregation operation will read from, must not be null or
empty.outputType - The parametrized type of the returned list, must not be null.public <O> CloseableIterator<O> aggregateStream(Aggregation aggregation, String collectionName, Class<O> outputType)
MongoOperationsAggregateIterable.
CloseableIterator that wraps the a Mongo DB AggregateIterable that
needs to be closed. The raw results will be mapped to the given entity class.
aggregation explain. Enabling
explanation mode will throw an IllegalArgumentException.aggregateStream in interface MongoOperationsaggregation - The Aggregation specification holding the aggregation operations, must not be
null.collectionName - the collection where the aggregation operation will read from, must not be null or
empty.outputType - The parametrized type of the returned list, must not be null.public <T> List<T> findAllAndRemove(Query query, String collectionName)
MongoOperationsMongoOperations.findAllAndRemove(Query, Class, String) to get full type specific support.findAllAndRemove in interface MongoOperationsquery - the query document that specifies the criteria used to find and remove documents.collectionName - name of the collection where the objects will removed, must not be null or empty.List converted objects deleted by this operation.public <T> List<T> findAllAndRemove(Query query, Class<T> entityClass)
MongoOperationsfindAllAndRemove in interface MongoOperationsquery - the query document that specifies the criteria used to find and remove documents.entityClass - class of the pojo to be operated on.List converted objects deleted by this operation.public <T> List<T> findAllAndRemove(Query query, Class<T> entityClass, String collectionName)
MongoOperationsfindAllAndRemove in interface MongoOperationsquery - the query document that specifies the criteria used to find and remove documents.entityClass - class of the pojo to be operated on.collectionName - name of the collection where the objects will removed, must not be null or empty.List converted objects deleted by this operation.protected <T> List<T> doFindAndDelete(String collectionName, Query query, Class<T> entityClass)
query by calling find(Query, Class, String)
and remove(Query, Class, String), whereas the Query for remove(Query, Class, String) is
constructed out of the find result.collectionName - query - entityClass - protected <O> AggregationResults<O> aggregate(Aggregation aggregation, String collectionName, Class<O> outputType, @Nullable AggregationOperationContext context)
protected <O> AggregationResults<O> doAggregate(Aggregation aggregation, String collectionName, Class<O> outputType, AggregationOperationContext context)
protected <O> CloseableIterator<O> aggregateStream(Aggregation aggregation, String collectionName, Class<O> outputType, @Nullable AggregationOperationContext context)
public <T> ExecutableFindOperation.ExecutableFind<T> query(Class<T> domainType)
ExecutableFindOperationquery in interface ExecutableFindOperationdomainType - must not be null.ExecutableFindOperation.ExecutableFind.public <T> ExecutableUpdateOperation.ExecutableUpdate<T> update(Class<T> domainType)
ExecutableUpdateOperationupdate in interface ExecutableUpdateOperationdomainType - must not be null.ExecutableUpdateOperation.ExecutableUpdate.public <T> ExecutableRemoveOperation.ExecutableRemove<T> remove(Class<T> domainType)
ExecutableRemoveOperationremove in interface ExecutableRemoveOperationdomainType - must not be null.ExecutableRemoveOperation.ExecutableRemove.public <T> ExecutableAggregationOperation.ExecutableAggregation<T> aggregateAndReturn(Class<T> domainType)
ExecutableAggregationOperationTypedAggregation to specify a potentially different
input type for he aggregation.aggregateAndReturn in interface ExecutableAggregationOperationdomainType - must not be null.ExecutableAggregationOperation.ExecutableAggregation.public <T> ExecutableMapReduceOperation.ExecutableMapReduce<T> mapReduce(Class<T> domainType)
ExecutableMapReduceOperationmapReduce in interface ExecutableMapReduceOperationdomainType - must not be null.ExecutableFindOperation.ExecutableFind.public <T> ExecutableInsertOperation.ExecutableInsert<T> insert(Class<T> domainType)
ExecutableInsertOperationinsert in interface ExecutableInsertOperationdomainType - must not be null.ExecutableInsertOperation.ExecutableInsert.public Set<String> getCollectionNames()
MongoOperationsgetCollectionNames in interface MongoOperationspublic com.mongodb.client.MongoDatabase getDb()
protected com.mongodb.client.MongoDatabase doGetDatabase()
protected com.mongodb.client.MongoDatabase prepareDatabase(com.mongodb.client.MongoDatabase database)
protected <E extends MongoMappingEvent<T>,T> E maybeEmitEvent(E event)
protected <T> T maybeCallBeforeConvert(T object,
String collection)
protected <T> T maybeCallBeforeSave(T object,
org.bson.Document document,
String collection)
protected <T> T maybeCallAfterSave(T object,
org.bson.Document document,
String collection)
protected <T> T maybeCallAfterConvert(T object,
org.bson.Document document,
String collection)
protected com.mongodb.client.MongoCollection<org.bson.Document> doCreateCollection(String collectionName, org.bson.Document collectionOptions)
collectionName - collectionOptions - protected <T> T doFindOne(String collectionName, org.bson.Document query, org.bson.Document fields, Class<T> entityClass)
Document and so is the fields specification.collectionName - name of the collection to retrieve the objects from.query - the query document that specifies the criteria used to find a record.fields - the document that specifies the fields to be returned.entityClass - the parameterized type of the returned list.List of converted objects.protected <T> T doFindOne(String collectionName, org.bson.Document query, org.bson.Document fields, CursorPreparer preparer, Class<T> entityClass)
Document and so is the fields specification.collectionName - name of the collection to retrieve the objects from.query - the query document that specifies the criteria used to find a record.fields - the document that specifies the fields to be returned.entityClass - the parameterized type of the returned list.preparer - the preparer used to modify the cursor on execution.List of converted objects.protected <T> List<T> doFind(String collectionName, org.bson.Document query, org.bson.Document fields, Class<T> entityClass)
collectionName - name of the collection to retrieve the objects fromquery - the query document that specifies the criteria used to find a recordfields - the document that specifies the fields to be returnedentityClass - the parameterized type of the returned list.protected <T> List<T> doFind(String collectionName, org.bson.Document query, org.bson.Document fields, Class<T> entityClass, CursorPreparer preparer)
MongoConverter. The query document is
specified as a standard Document and so is the fields specification.collectionName - name of the collection to retrieve the objects from.query - the query document that specifies the criteria used to find a record.fields - the document that specifies the fields to be returned.entityClass - the parameterized type of the returned list.preparer - allows for customization of the FindIterable used when iterating over the result set,
(apply limits, skips and so on).List of converted objects.protected <S,T> List<T> doFind(String collectionName, org.bson.Document query, org.bson.Document fields, Class<S> entityClass, @Nullable CursorPreparer preparer, org.springframework.data.mongodb.core.MongoTemplate.DocumentCallback<T> objectCallback)
protected org.bson.Document convertToDocument(@Nullable CollectionOptions collectionOptions, Class<?> targetType)
CollectionOptions to a document and take the domain type information into account when
creating a mapped schema for validation. convertToDocument(CollectionOptions) for backwards compatibility and potentially
overwrites the validator with the mapped validator document. In the long run
convertToDocument(CollectionOptions) will be removed so that this one becomes the only source of truth.collectionOptions - can be null.targetType - must not be null. Use Object type instead.@Deprecated protected org.bson.Document convertToDocument(@Nullable CollectionOptions collectionOptions)
convertToDocument(CollectionOptions, Class).collectionOptions - can be null.protected <T> T doFindAndRemove(String collectionName, org.bson.Document query, org.bson.Document fields, org.bson.Document sort, @Nullable Collation collation, Class<T> entityClass)
collectionName - name of the collection to retrieve the objects fromquery - the query document that specifies the criteria used to find a recordentityClass - the parameterized type of the returned list.protected <T> T doFindAndModify(String collectionName, org.bson.Document query, org.bson.Document fields, org.bson.Document sort, Class<T> entityClass, UpdateDefinition update, @Nullable FindAndModifyOptions options)
@Nullable protected <T> T doFindAndReplace(String collectionName, org.bson.Document mappedQuery, org.bson.Document mappedFields, org.bson.Document mappedSort, @Nullable com.mongodb.client.model.Collation collation, Class<?> entityType, org.bson.Document replacement, FindAndReplaceOptions options, Class<T> resultType)
collectionName - The name of the collection to perform the operation in.mappedQuery - the query to look up documents.mappedFields - the fields to project the result to.mappedSort - the sort to be applied when executing the query.collation - collation settings for the query. Can be null.entityType - the source domain type.replacement - the replacement Document.options - applicable options.resultType - the target domain type.return new is
false and upsert is false.protected <T> T populateIdIfNecessary(T savedObject,
Object id)
savedObject - id - public PersistenceExceptionTranslator getExceptionTranslator()
@Deprecated public MongoDatabaseFactory getMongoDbFactory()
getMongoDatabaseFactory() instead.MongoDatabaseFactory in use.public MongoDatabaseFactory getMongoDatabaseFactory()
MongoDatabaseFactory in use.Copyright © 2011–2022 Pivotal Software, Inc.. All rights reserved.