public interface MongoOperations extends FluentMongoOperations
MongoTemplate
. Not often used but
a useful option for extensibility and testability (as it can be easily mocked, stubbed, or be the target of a JDK
proxy).
NOTE: Some operations cannot be executed within a MongoDB transaction. Please refer to the MongoDB
specific documentation to learn more about Multi
Document Transactions.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>
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.
|
<O> AggregationResults<O> |
aggregate(TypedAggregation<?> aggregation,
Class<O> outputType)
Execute an aggregation operation.
|
<O> AggregationResults<O> |
aggregate(TypedAggregation<?> aggregation,
String collectionName,
Class<O> outputType)
Execute an aggregation operation.
|
<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 . |
<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 collectionName,
Class<O> outputType)
Execute an aggregation operation backed by a Mongo DB
AggregateIterable . |
BulkOperations |
bulkOps(BulkOperations.BulkMode mode,
Class<?> entityType)
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 mode,
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.
|
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.
|
<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.
|
default long |
estimatedCount(Class<?> entityClass)
Estimate the number of documents, in the collection
identified by the given type ,
based on collection statistics. |
long |
estimatedCount(String collectionName)
Estimate the number of documents in the given collection based on collection statistics.
|
<T> T |
execute(Class<?> entityClass,
CollectionCallback<T> action)
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> action)
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.
|
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.
|
default <S,T> T |
findAndReplace(Query query,
S replacement,
FindAndReplaceOptions options,
Class<S> entityType,
Class<T> resultType)
Triggers
findOneAndReplace
to replace a single document matching
Criteria of given Query with the replacement document
taking FindAndReplaceOptions into account. |
<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. |
default <T> T |
findAndReplace(Query query,
T replacement)
Triggers
findOneAndReplace
to replace a single document matching
Criteria of given Query with the replacement
document. |
default <T> T |
findAndReplace(Query query,
T replacement,
FindAndReplaceOptions options)
Triggers
findOneAndReplace
to replace a single document matching
Criteria of given Query with the replacement document
taking FindAndReplaceOptions into account. |
default <T> T |
findAndReplace(Query query,
T replacement,
FindAndReplaceOptions options,
Class<T> entityType,
String collectionName)
Triggers
findOneAndReplace
to replace a single document matching
Criteria of given Query with the replacement document
taking FindAndReplaceOptions into account. |
default <T> T |
findAndReplace(Query query,
T replacement,
FindAndReplaceOptions options,
String collectionName)
Triggers
findOneAndReplace
to replace a single document matching
Criteria of given Query with the replacement document
taking FindAndReplaceOptions into account. |
default <T> T |
findAndReplace(Query query,
T replacement,
String collectionName)
Triggers
findOneAndReplace
to replace a single document matching
Criteria of given Query with the replacement
document. |
<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 . |
default <T> List<T> |
findDistinct(Query query,
String field,
String collection,
Class<T> resultClass)
Finds the distinct values for a specified field across a single
MongoCollection or view and
returns the results in a List . |
default <T> List<T> |
findDistinct(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> 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<T> entityClass)
Deprecated.
since 2.2. The
eval command has been removed in MongoDB Server 4.2.0. Use Aggregations with
Aggregation.geoNear(NearQuery, String) instead. |
<T> GeoResults<T> |
geoNear(NearQuery near,
Class<T> entityClass,
String collectionName)
Deprecated.
since 2.2. The
eval command has been removed in MongoDB Server 4.2.0. Use Aggregations with
Aggregation.geoNear(NearQuery, String) instead. |
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 underlying
MongoConverter . |
<T> GroupByResults<T> |
group(Criteria criteria,
String inputCollectionName,
GroupBy groupBy,
Class<T> entityClass)
Deprecated.
since 2.2. The
group command has been removed in MongoDB Server 4.2.0. Please use aggregate(TypedAggregation, String, Class) with a
GroupOperation and
MatchOperation instead. |
<T> GroupByResults<T> |
group(String inputCollectionName,
GroupBy groupBy,
Class<T> entityClass)
Deprecated.
since 2.2. The
group command has been removed in MongoDB Server 4.2.0. Please use aggregate(TypedAggregation, String, Class) with a
GroupOperation instead. |
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
|
<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.
|
<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.
|
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.
|
<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.
|
ScriptOperations |
scriptOps()
Deprecated.
since 2.2. The
eval command has been removed without replacement in MongoDB Server 4.2.0. |
<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 . |
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.
|
MongoOperations |
withSession(com.mongodb.client.ClientSession session)
Obtain a
ClientSession bound instance of MongoOperations . |
SessionScoped |
withSession(com.mongodb.ClientSessionOptions sessionOptions)
Obtain a
session bound instance of SessionScoped binding a new ClientSession
with given sessionOptions to each and every command issued against MongoDB. |
default SessionScoped |
withSession(Supplier<com.mongodb.client.ClientSession> sessionProvider)
Obtain a
session bound instance of SessionScoped binding the ClientSession
provided by the given Supplier to each and every command issued against MongoDB. |
query
insert
update
remove
aggregateAndReturn
mapReduce
String getCollectionName(Class<?> entityClass)
entityClass
- must not be null.org.bson.Document executeCommand(String jsonCommand)
Document.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.jsonCommand
- a MongoDB command expressed as a JSON string. Must not be null.org.bson.Document executeCommand(org.bson.Document command)
command
- a MongoDB command.org.bson.Document executeCommand(org.bson.Document command, @Nullable com.mongodb.ReadPreference readPreference)
command
- a MongoDB command, must not be null.readPreference
- read preferences to use, can be null.void executeQuery(Query query, String collectionName, DocumentCallbackHandler dch)
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 from.dch
- the handler that will extract results, one document at a time.@Nullable <T> T execute(DbCallback<T> action)
DbCallback
translating any exceptions as necessary.
Allows for returning a result object, that is a domain object or a collection of domain objects.T
- return type.action
- callback object that specifies the MongoDB actions to perform on the passed in DB instance. Must not
be null.@Nullable <T> T execute(Class<?> entityClass, CollectionCallback<T> action)
CollectionCallback
on the entity collection of the specified class.
Allows for returning a result object, that is a domain object or a collection of domain objects.T
- return type.entityClass
- class that determines the collection to use. Must not be null.action
- callback object that specifies the MongoDB action. Must not be null.@Nullable <T> T execute(String collectionName, CollectionCallback<T> action)
CollectionCallback
on the collection of the given name.
Allows for returning a result object, that is a domain object or a collection of domain objects.T
- return type.collectionName
- the name of the collection that specifies which MongoCollection
instance will be
passed into. Must not be null or empty.action
- callback object that specifies the MongoDB action the callback action. Must not be null.SessionScoped withSession(com.mongodb.ClientSessionOptions sessionOptions)
session
bound instance of SessionScoped
binding a new ClientSession
with given sessionOptions to each and every command issued against MongoDB.sessionOptions
- must not be null.SessionScoped
. Never null.default SessionScoped withSession(Supplier<com.mongodb.client.ClientSession> sessionProvider)
session
bound instance of SessionScoped
binding the ClientSession
provided by the given Supplier
to each and every command issued against MongoDB.
Note: It is up to the caller to manage the ClientSession
lifecycle. Use the
SessionScoped.execute(SessionCallback, Consumer)
hook to potentially close the ClientSession
.sessionProvider
- must not be null.MongoOperations withSession(com.mongodb.client.ClientSession session)
ClientSession
bound instance of MongoOperations
.
Note: It is up to the caller to manage the ClientSession
lifecycle.session
- must not be null.ClientSession
bound instance of MongoOperations
.<T> CloseableIterator<T> stream(Query query, Class<T> entityType)
Query
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.
T
- 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.<T> CloseableIterator<T> stream(Query query, Class<T> entityType, String collectionName)
Query
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.
T
- 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.<T> com.mongodb.client.MongoCollection<org.bson.Document> createCollection(Class<T> entityClass)
entityClass
- class that determines the collection to create.<T> com.mongodb.client.MongoCollection<org.bson.Document> createCollection(Class<T> entityClass, @Nullable CollectionOptions collectionOptions)
entityClass
- class that determines the collection to create. Must not be null.collectionOptions
- options to use when creating the collection.com.mongodb.client.MongoCollection<org.bson.Document> createCollection(String collectionName)
collectionName
- name of the collection.com.mongodb.client.MongoCollection<org.bson.Document> createCollection(String collectionName, @Nullable CollectionOptions collectionOptions)
collectionName
- name of the collection. Must not be null nor empty.collectionOptions
- options to use when creating the collection.Set<String> getCollectionNames()
com.mongodb.client.MongoCollection<org.bson.Document> getCollection(String collectionName)
MongoCollection
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
createCollection(Class)
. Please make sure to check if the collection exists
first.
Translate any exceptions as necessary.collectionName
- name of the collection. Must not be null.<T> boolean collectionExists(Class<T> entityClass)
entityClass
- class that determines the name of the collection. Must not be null.boolean collectionExists(String collectionName)
collectionName
- name of the collection. Must not be null.<T> void dropCollection(Class<T> entityClass)
entityClass
- class that determines the collection to drop/delete. Must not be null.void dropCollection(String collectionName)
collectionName
- name of the collection to drop/delete.IndexOperations indexOps(String collectionName)
IndexOperations indexOps(Class<?> entityClass)
@Deprecated ScriptOperations scriptOps()
eval
command has been removed without replacement in MongoDB Server 4.2.0.ScriptOperations
that can be performed on MongoDatabase
level.BulkOperations bulkOps(BulkOperations.BulkMode mode, String collectionName)
BulkOperations
for the given collection. #bulkOps(BulkMode, Class, String)
to get full type specific support.mode
- 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 collectionBulkOperations bulkOps(BulkOperations.BulkMode mode, Class<?> entityType)
BulkOperations
for the given entity type.mode
- the BulkOperations.BulkMode
to use for bulk operations, must not be null.entityType
- the name of the entity class, must not be null.BulkOperations
on the named collection associated of the given entity class.BulkOperations bulkOps(BulkOperations.BulkMode mode, @Nullable Class<?> entityType, String collectionName)
BulkOperations
for the given entity type and collection name.mode
- 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.<T> List<T> findAll(Class<T> entityClass)
MongoConverter
. Unless
configured otherwise, an instance of MappingMongoConverter
will be used.
If your collection does not contain a homogeneous collection of types, this operation will not be an efficient way
to map objects since the test for class type is done in the client and not on the server.entityClass
- the parametrized type of the returned list.<T> List<T> findAll(Class<T> entityClass, String collectionName)
MongoConverter
. Unless
configured otherwise, an instance of MappingMongoConverter
will be used.
If your collection does not contain a homogeneous collection of types, this operation will not be an efficient way
to map objects since the test for class type is done in the client and not on the server.entityClass
- the parametrized type of the returned list.collectionName
- name of the collection to retrieve the objects from.@Deprecated <T> GroupByResults<T> group(String inputCollectionName, GroupBy groupBy, Class<T> entityClass)
group
command has been removed in MongoDB Server 4.2.0. aggregate(TypedAggregation, String, Class)
with a
GroupOperation
instead.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 list@Deprecated <T> GroupByResults<T> group(@Nullable Criteria criteria, String inputCollectionName, GroupBy groupBy, Class<T> entityClass)
group
command has been removed in MongoDB Server 4.2.0. aggregate(TypedAggregation, String, Class)
with a
GroupOperation
and
MatchOperation
instead.criteria
- 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 list<O> AggregationResults<O> aggregate(TypedAggregation<?> aggregation, String collectionName, Class<O> outputType)
aggregation
- The TypedAggregation
specification holding the aggregation operations, must not be
null.collectionName
- The name of the input collection to use for the aggreation.outputType
- The parametrized type of the returned list, must not be null.<O> AggregationResults<O> aggregate(TypedAggregation<?> aggregation, Class<O> outputType)
aggregation
- The TypedAggregation
specification holding the aggregation operations, must not be
null.outputType
- The parametrized type of the returned list, must not be null.<O> AggregationResults<O> aggregate(Aggregation aggregation, Class<?> inputType, Class<O> outputType)
aggregation
- 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.<O> AggregationResults<O> aggregate(Aggregation aggregation, String collectionName, Class<O> outputType)
aggregation
- 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.<O> CloseableIterator<O> aggregateStream(TypedAggregation<?> aggregation, String collectionName, Class<O> outputType)
AggregateIterable
.
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
.
aggregation
- The TypedAggregation
specification holding the aggregation operations, must not be
null.collectionName
- The name of the input collection to use for the aggreation.outputType
- The parametrized type of the returned list, must not be null.<O> CloseableIterator<O> aggregateStream(TypedAggregation<?> aggregation, Class<O> outputType)
AggregateIterable
.
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 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 aggregation explain
. Enabling
explanation mode will throw an IllegalArgumentException
.aggregation
- The TypedAggregation
specification holding the aggregation operations, must not be
null.outputType
- The parametrized type of the returned list, must not be null.<O> CloseableIterator<O> aggregateStream(Aggregation aggregation, Class<?> inputType, Class<O> outputType)
AggregateIterable
.
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.
Aggregation streaming can't be used with aggregation explain
. Enabling
explanation mode will throw an IllegalArgumentException
.aggregation
- 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.<O> CloseableIterator<O> aggregateStream(Aggregation aggregation, String collectionName, Class<O> outputType)
AggregateIterable
.
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.
Aggregation streaming can't be used with aggregation explain
. Enabling
explanation mode will throw an IllegalArgumentException
.aggregation
- 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.<T> MapReduceResults<T> mapReduce(String inputCollectionName, String mapFunction, String reduceFunction, Class<T> entityClass)
inputCollectionName
- 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.<T> MapReduceResults<T> mapReduce(String inputCollectionName, String mapFunction, String reduceFunction, @Nullable MapReduceOptions mapReduceOptions, Class<T> entityClass)
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 behavior.entityClass
- The parametrized type of the returned list. Must not be null.<T> MapReduceResults<T> mapReduce(Query query, String inputCollectionName, String mapFunction, String reduceFunction, Class<T> entityClass)
query
- 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.<T> MapReduceResults<T> mapReduce(Query query, String inputCollectionName, String mapFunction, String reduceFunction, @Nullable MapReduceOptions mapReduceOptions, Class<T> entityClass)
query
- 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.@Deprecated <T> GeoResults<T> geoNear(NearQuery near, Class<T> entityClass)
eval
command has been removed in MongoDB Server 4.2.0. Use Aggregations with
Aggregation.geoNear(NearQuery, String)
instead.GeoResults
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);
near
- must not be null.entityClass
- must not be null.@Deprecated <T> GeoResults<T> geoNear(NearQuery near, Class<T> entityClass, String collectionName)
eval
command has been removed in MongoDB Server 4.2.0. Use Aggregations with
Aggregation.geoNear(NearQuery, String)
instead.GeoResults
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);
near
- must not be null.entityClass
- 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.@Nullable <T> T findOne(Query query, Class<T> entityClass)
MongoConverter
. Unless
configured otherwise, an instance of MappingMongoConverter
will be used.
The query is specified as a Query
which can be created either using the BasicQuery
or the more
feature rich Query
.query
- 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 <T> T findOne(Query query, Class<T> entityClass, String collectionName)
MongoConverter
. Unless
configured otherwise, an instance of MappingMongoConverter
will be used.
The query is specified as a Query
which can be created either using the BasicQuery
or the more
feature rich Query
.query
- 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.boolean exists(Query query, String collectionName)
Query
contains at least one element. exists(Query, Class, String)
to get full type specific support.query
- the Query
class that specifies the criteria used to find a record.collectionName
- name of the collection to check for objects.boolean exists(Query query, Class<?> entityClass)
Query
contains at least one element.query
- the Query
class that specifies the criteria used to find a record.entityClass
- the parametrized type.boolean exists(Query query, @Nullable Class<?> entityClass, String collectionName)
Query
contains at least one element.query
- 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.<T> List<T> find(Query query, Class<T> entityClass)
MongoConverter
. Unless
configured otherwise, an instance of MappingMongoConverter
will be used.
The query is specified as a Query
which can be created either using the BasicQuery
or the more
feature rich Query
.query
- 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.<T> List<T> find(Query query, Class<T> entityClass, String collectionName)
MongoConverter
. Unless
configured otherwise, an instance of MappingMongoConverter
will be used.
The query is specified as a Query
which can be created either using the BasicQuery
or the more
feature rich Query
.query
- 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 <T> T findById(Object id, Class<T> entityClass)
id
- 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 <T> T findById(Object id, Class<T> entityClass, String collectionName)
id
- the id of the document to return.entityClass
- the type to convert the document to.collectionName
- the collection to query for the document.default <T> List<T> findDistinct(String field, Class<?> entityClass, Class<T> resultClass)
MongoCollection
or view and
returns the results in a List
.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
. Must not be
null.resultClass
- the result type. Must not be null.<T> List<T> findDistinct(Query query, String field, Class<?> entityClass, Class<T> resultClass)
MongoCollection
or view and
returns the results in a List
.query
- 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.<T> List<T> findDistinct(Query query, String field, String collectionName, Class<?> entityClass, Class<T> resultClass)
MongoCollection
or view and
returns the results in a List
.query
- 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.default <T> List<T> findDistinct(Query query, String field, String collection, Class<T> resultClass)
MongoCollection
or view and
returns the results in a List
.query
- filter Query
to restrict search. Must not be null.field
- the name of the field to inspect for distinct values. Must not be null.collection
- the explicit name of the actual MongoCollection
. Must not be null.resultClass
- the result type. Must not be null.@Nullable <T> T findAndModify(Query query, UpdateDefinition update, Class<T> entityClass)
query
- 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 <T> T findAndModify(Query query, UpdateDefinition update, Class<T> entityClass, String collectionName)
query
- 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 <T> T findAndModify(Query query, UpdateDefinition update, FindAndModifyOptions options, Class<T> entityClass)
Update
on documents matching Criteria
of given Query
taking
FindAndModifyOptions
into account.query
- 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 <T> T findAndModify(Query query, UpdateDefinition update, FindAndModifyOptions options, Class<T> entityClass, String collectionName)
Update
on documents matching Criteria
of given Query
taking
FindAndModifyOptions
into account.query
- 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
,
AggregationUpdate
@Nullable default <T> T findAndReplace(Query query, T replacement)
Criteria
of given Query
with the replacement
document. FindAndReplaceOptions.empty()
. @Nullable default <T> T findAndReplace(Query query, T replacement, String collectionName)
Criteria
of given Query
with the replacement
document.FindAndReplaceOptions.empty()
. query
- 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.collectionName
- the collection to query. Must not be null.@Nullable default <T> T findAndReplace(Query query, T replacement, FindAndReplaceOptions options)
Criteria
of given Query
with the replacement
document
taking FindAndReplaceOptions
into account.query
- 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.FindAndReplaceOptions.isReturnNew()
this will either be the object as it was before the update or
as it is after the update.@Nullable default <T> T findAndReplace(Query query, T replacement, FindAndReplaceOptions options, String collectionName)
Criteria
of given Query
with the replacement
document
taking FindAndReplaceOptions
into account.query
- 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.FindAndReplaceOptions.isReturnNew()
this will either be the object as it was before the update or
as it is after the update.@Nullable default <T> T findAndReplace(Query query, T replacement, FindAndReplaceOptions options, Class<T> entityType, String collectionName)
Criteria
of given Query
with the replacement
document
taking FindAndReplaceOptions
into account.query
- 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 parametrized type. Must not be null.collectionName
- the collection to query. Must not be null.FindAndReplaceOptions.isReturnNew()
this will either be the object as it was before the update or
as it is after the update.@Nullable default <S,T> T findAndReplace(Query query, S replacement, FindAndReplaceOptions options, Class<S> entityType, Class<T> resultType)
Criteria
of given Query
with the replacement
document
taking FindAndReplaceOptions
into account.query
- 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 and deriving the collection
from. 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 <S,T> T findAndReplace(Query query, S replacement, FindAndReplaceOptions options, Class<S> entityType, String collectionName, Class<T> resultType)
Criteria
of given Query
with the replacement
document
taking FindAndReplaceOptions
into account.query
- 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 <T> T findAndRemove(Query query, Class<T> entityClass)
MongoConverter
.
The query is specified as a Query
which can be created either using the BasicQuery
or the more
feature rich Query
.query
- 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 <T> T findAndRemove(Query query, Class<T> entityClass, String collectionName)
MongoConverter
. Unless
configured otherwise, an instance of MappingMongoConverter
will be used.
The query is specified as a Query
which can be created either using the BasicQuery
or the more
feature rich Query
.query
- 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.long count(Query query, Class<?> entityClass)
Query
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.
This method uses an
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
estimatedCount(Class)
for empty queries instead.query
- 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.long count(Query query, String collectionName)
Query
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 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.
This method uses an
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
estimatedCount(String)
for empty queries instead.query
- the Query
class that specifies the criteria used to find documents.collectionName
- must not be null or empty.count(Query, Class, String)
default long estimatedCount(Class<?> entityClass)
identified by the given type
,
based on collection statistics.
Please make sure to read the MongoDB reference documentation about limitations on eg. sharded cluster or inside
transactions.entityClass
- must not be null.long estimatedCount(String collectionName)
collectionName
- must not be null.long count(Query query, @Nullable Class<?> entityClass, String collectionName)
Query
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.
This method uses an
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
estimatedCount(String)
for empty queries instead.query
- 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.<T> T insert(T objectToSave)
MongoConverter
.
If your object has an "Id' property, it will be set with the generated Id from MongoDB. If your Id property is a
String then MongoDB ObjectId will be used to populate that string. Otherwise, the conversion from ObjectId to your
property type will be handled by Spring's BeanWrapper class that leverages Type Conversion API. See
Spring's
Type Conversion" for more details.
Insert is used to initially store the object into the database. To update an existing object use the save method.
The objectToSave
must not be collection-like.objectToSave
- the object to store in the collection. Must not be null.IllegalArgumentException
- in case the objectToSave
is collection-like.<T> T insert(T objectToSave, String collectionName)
MongoConverter
. Unless
configured otherwise, an instance of MappingMongoConverter
will be used.
Insert is used to initially store the object into the database. To update an existing object use the save method.
The objectToSave
must not be collection-like.objectToSave
- 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.IllegalArgumentException
- in case the objectToSave
is collection-like.<T> Collection<T> insert(Collection<? extends T> batchToSave, Class<?> entityClass)
batchToSave
- the batch of objects to save. Must not be null.entityClass
- class that determines the collection to use. Must not be null.<T> Collection<T> insert(Collection<? extends T> batchToSave, String collectionName)
batchToSave
- the list of objects to save. Must not be null.collectionName
- name of the collection to store the object in. Must not be null.<T> Collection<T> insertAll(Collection<? extends T> objectsToSave)
objectsToSave
- the list of objects to save. Must not be null.<T> T save(T objectToSave)
MongoConverter
. Unless
configured otherwise, an instance of MappingMongoConverter
will be used.
If your object has an "Id' property, it will be set with the generated Id from MongoDB. If your Id property is a
String then MongoDB ObjectId will be used to populate that string. Otherwise, the conversion from ObjectId to your
property type will be handled by Spring's BeanWrapper class that leverages Type Conversion API. See
Spring's
Type Conversion" for more details.
The objectToSave
must not be collection-like.objectToSave
- the object to store in the collection. Must not be null.IllegalArgumentException
- in case the objectToSave
is collection-like.<T> T save(T objectToSave, String collectionName)
MongoConverter
. Unless
configured otherwise, an instance of MappingMongoConverter
will be used.
If your object has an "Id' property, it will be set with the generated Id from MongoDB. If your Id property is a
String then MongoDB ObjectId will be used to populate that string. Otherwise, the conversion from ObjectId to your
property type will be handled by Spring's BeanWrapper class that leverages Type Conversion API. See Spring's Type
Conversion" for more details.
The objectToSave
must not be collection-like.objectToSave
- 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.IllegalArgumentException
- in case the objectToSave
is collection-like.com.mongodb.client.result.UpdateResult upsert(Query query, UpdateDefinition update, Class<?> entityClass)
sorting
is not supported by db.collection.updateOne
.
Use findAndModify(Query, UpdateDefinition, FindAndModifyOptions, Class, String)
instead.query
- 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
,
AggregationUpdate
com.mongodb.client.result.UpdateResult upsert(Query query, UpdateDefinition update, String collectionName)
upsert(Query, UpdateDefinition, Class, String)
to get full type specific
support. sorting
is not supported by db.collection.updateOne
.
Use findAndModify(Query, UpdateDefinition, FindAndModifyOptions, Class, String)
instead.query
- 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
,
AggregationUpdate
com.mongodb.client.result.UpdateResult upsert(Query query, UpdateDefinition update, Class<?> entityClass, String collectionName)
query
- 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
,
AggregationUpdate
com.mongodb.client.result.UpdateResult updateFirst(Query query, UpdateDefinition update, Class<?> entityClass)
query
- 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
,
AggregationUpdate
com.mongodb.client.result.UpdateResult updateFirst(Query query, UpdateDefinition update, String collectionName)
updateFirst(Query, UpdateDefinition, Class, String)
to get full type specific
support. sorting
is not supported by db.collection.updateOne
.
Use findAndModify(Query, UpdateDefinition, Class, String)
instead.query
- 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
,
AggregationUpdate
com.mongodb.client.result.UpdateResult updateFirst(Query query, UpdateDefinition update, Class<?> entityClass, String collectionName)
query
- 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
,
AggregationUpdate
com.mongodb.client.result.UpdateResult updateMulti(Query query, UpdateDefinition update, Class<?> entityClass)
query
- 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
,
AggregationUpdate
com.mongodb.client.result.UpdateResult updateMulti(Query query, UpdateDefinition update, String collectionName)
updateMulti(Query, UpdateDefinition, Class, String)
to get full type specific
support.query
- 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
,
AggregationUpdate
com.mongodb.client.result.UpdateResult updateMulti(Query query, UpdateDefinition update, Class<?> entityClass, String collectionName)
query
- 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
,
AggregationUpdate
com.mongodb.client.result.DeleteResult remove(Object object)
Version
. DeleteResult.getDeletedCount()
for insight whether an acknowledged
remove operation was successful or not.object
- must not be null.DeleteResult
which lets you access the results of the previous delete.com.mongodb.client.result.DeleteResult remove(Object object, String collectionName)
Version
. DeleteResult.getDeletedCount()
for insight whether an acknowledged
remove operation was successful or not.object
- 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.com.mongodb.client.result.DeleteResult remove(Query query, Class<?> entityClass)
query
- 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.IllegalArgumentException
- when query or entityClass is null.com.mongodb.client.result.DeleteResult remove(Query query, Class<?> entityClass, String collectionName)
query
- 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.IllegalArgumentException
- when query, entityClass or collectionName is
null.com.mongodb.client.result.DeleteResult remove(Query query, String collectionName)
remove(Query, Class, String)
to get full type specific support.query
- 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.IllegalArgumentException
- when query or collectionName is null.<T> List<T> findAllAndRemove(Query query, String collectionName)
findAllAndRemove(Query, Class, String)
to get full type specific support.query
- 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.<T> List<T> findAllAndRemove(Query query, Class<T> entityClass)
query
- 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.<T> List<T> findAllAndRemove(Query query, Class<T> entityClass, String collectionName)
query
- 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.MongoConverter getConverter()
MongoConverter
.Copyright © 2011–2021 Pivotal Software, Inc.. All rights reserved.