public class MongoTemplate extends Object implements MongoOperations, ApplicationContextAware
MongoOperations.| Constructor and Description |
|---|
MongoTemplate(MongoDbFactory mongoDbFactory)
Constructor used for a basic template configuration.
|
MongoTemplate(MongoDbFactory mongoDbFactory,
MongoConverter mongoConverter)
Constructor used for a basic template configuration.
|
MongoTemplate(com.mongodb.Mongo mongo,
String databaseName)
Constructor used for a basic template configuration
|
MongoTemplate(com.mongodb.Mongo mongo,
String databaseName,
UserCredentials userCredentials)
Constructor used for a template configuration with user credentials in the form of
UserCredentials |
| 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.
|
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 com.mongodb.DBObject |
convertToDbObject(CollectionOptions collectionOptions) |
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.DBCollection |
createCollection(Class<T> entityClass)
Create an uncapped collection with a name based on the provided entity class.
|
<T> com.mongodb.DBCollection |
createCollection(Class<T> entityClass,
CollectionOptions collectionOptions)
Create a collection with a name based on the provided entity class using the options.
|
com.mongodb.DBCollection |
createCollection(String collectionName)
Create an uncapped collection with the provided name.
|
com.mongodb.DBCollection |
createCollection(String collectionName,
CollectionOptions collectionOptions)
Create a collection with the provided name and options.
|
protected com.mongodb.DBCollection |
doCreateCollection(String collectionName,
com.mongodb.DBObject collectionOptions)
Create the specified collection using the provided options
|
protected <S,T> List<T> |
doFind(String collectionName,
com.mongodb.DBObject query,
com.mongodb.DBObject fields,
Class<S> entityClass,
org.springframework.data.mongodb.core.CursorPreparer preparer,
org.springframework.data.mongodb.core.MongoTemplate.DbObjectCallback<T> objectCallback) |
protected <T> List<T> |
doFind(String collectionName,
com.mongodb.DBObject query,
com.mongodb.DBObject 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,
com.mongodb.DBObject query,
com.mongodb.DBObject fields,
Class<T> entityClass,
org.springframework.data.mongodb.core.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,
com.mongodb.DBObject query,
com.mongodb.DBObject fields,
com.mongodb.DBObject sort,
Class<T> entityClass,
Update update,
FindAndModifyOptions options) |
protected <T> T |
doFindAndRemove(String collectionName,
com.mongodb.DBObject query,
com.mongodb.DBObject fields,
com.mongodb.DBObject sort,
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,
com.mongodb.DBObject query,
com.mongodb.DBObject 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> void |
doInsert(String collectionName,
T objectToSave,
MongoWriter<T> writer) |
protected <T> void |
doInsertAll(Collection<? extends T> listToSave,
MongoWriter<T> writer) |
protected <T> void |
doInsertBatch(String collectionName,
Collection<? extends T> batchToSave,
MongoWriter<T> writer) |
protected <T> com.mongodb.WriteResult |
doRemove(String collectionName,
Query query,
Class<T> entityClass) |
protected <T> void |
doSave(String collectionName,
T objectToSave,
MongoWriter<T> writer) |
protected com.mongodb.WriteResult |
doUpdate(String collectionName,
Query query,
Update 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 |
ensureNotIterable(Object o) |
<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. |
com.mongodb.CommandResult |
executeCommand(com.mongodb.DBObject command)
Execute a MongoDB command.
|
com.mongodb.CommandResult |
executeCommand(com.mongodb.DBObject command,
int options)
Deprecated.
|
com.mongodb.CommandResult |
executeCommand(com.mongodb.DBObject command,
com.mongodb.ReadPreference readPreference)
Execute a MongoDB command.
|
com.mongodb.CommandResult |
executeCommand(String jsonCommand)
Execute the a MongoDB command expressed as a JSON string.
|
<T> T |
executeInSession(DbCallback<T> action)
Deprecated.
|
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 dch,
org.springframework.data.mongodb.core.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,
Update update,
Class<T> entityClass)
|
<T> T |
findAndModify(Query query,
Update update,
Class<T> entityClass,
String collectionName)
|
<T> T |
findAndModify(Query query,
Update 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,
Update 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.
|
<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> 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)
Returns
GeoResults for all entities matching the given NearQuery. |
<T> GeoResults<T> |
geoNear(NearQuery near,
Class<T> entityClass,
String collectionName)
Returns
GeoResults for all entities matching the given NearQuery. |
com.mongodb.DBCollection |
getCollection(String collectionName)
Get a collection by name, creating it if it doesn't exist.
|
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.DB |
getDb() |
<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.
|
protected void |
handleAnyWriteResultErrors(com.mongodb.WriteResult writeResult,
com.mongodb.DBObject query,
MongoActionOperation operation)
Handles
WriteResult errors based on the configured WriteResultChecking. |
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
|
void |
insert(Collection<? extends Object> batchToSave,
Class<?> entityClass)
Insert a Collection of objects into a collection in a single batch write to the database.
|
void |
insert(Collection<? extends Object> batchToSave,
String collectionName)
Insert a list of objects into the specified collection in a single batch write to the database.
|
void |
insert(Object objectToSave)
Insert the object into the collection for the entity type of the object to save.
|
void |
insert(Object objectToSave,
String collectionName)
Insert the object into the specified collection.
|
void |
insertAll(Collection<? extends Object> objectsToSave)
Insert a mixed Collection of objects into a database collection determining the collection name to use based on the
class.
|
protected Object |
insertDBObject(String collectionName,
com.mongodb.DBObject dbDoc,
Class<?> entityClass) |
protected List<org.bson.types.ObjectId> |
insertDBObjectList(String collectionName,
List<com.mongodb.DBObject> dbDocList) |
protected void |
logCommandExecutionError(com.mongodb.DBObject command,
com.mongodb.CommandResult result) |
<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> void |
maybeEmitEvent(MongoMappingEvent<T> event) |
protected void |
populateIdIfNecessary(Object savedObject,
Object id)
Populates the id property of the saved object, if it's not set already.
|
protected void |
prepareCollection(com.mongodb.DBCollection collection)
Prepare the collection before any processing is done using it.
|
protected com.mongodb.WriteConcern |
prepareWriteConcern(MongoAction mongoAction)
Prepare the WriteConcern before any processing is done using it.
|
com.mongodb.WriteResult |
remove(Object object)
Remove the given object from the collection by id.
|
com.mongodb.WriteResult |
remove(Object object,
String collection)
Removes the given object from the given collection.
|
com.mongodb.WriteResult |
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.WriteResult |
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.WriteResult |
remove(Query query,
String collectionName)
Remove all documents from the specified collection that match the provided query document criteria.
|
protected String |
replaceWithResourceIfNecessary(String function) |
void |
save(Object objectToSave)
Save the object to the collection for the entity type of the object to save.
|
void |
save(Object objectToSave,
String collectionName)
Save the object to the specified collection.
|
protected Object |
saveDBObject(String collectionName,
com.mongodb.DBObject dbDoc,
Class<?> entityClass) |
ScriptOperations |
scriptOps()
Returns the
ScriptOperations that can be performed on DB level. |
void |
setApplicationContext(ApplicationContext applicationContext) |
void |
setReadPreference(com.mongodb.ReadPreference readPreference)
Used by @{link
prepareCollection(DBCollection) to set the ReadPreference before any operations are
performed. |
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
Cursor. |
<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 Cursor. |
com.mongodb.WriteResult |
updateFirst(Query query,
Update 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.WriteResult |
updateFirst(Query query,
Update 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.WriteResult |
updateFirst(Query query,
Update 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.WriteResult |
updateMulti(Query query,
Update 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.WriteResult |
updateMulti(Query query,
Update 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.WriteResult |
updateMulti(Query query,
Update 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.WriteResult |
upsert(Query query,
Update update,
Class<?> entityClass)
Performs an upsert.
|
com.mongodb.WriteResult |
upsert(Query query,
Update update,
Class<?> entityClass,
String collectionName)
Performs an upsert.
|
com.mongodb.WriteResult |
upsert(Query query,
Update update,
String collectionName)
Performs an upsert.
|
public MongoTemplate(com.mongodb.Mongo mongo,
String databaseName)
mongo - must not be null.databaseName - must not be null or empty.public MongoTemplate(com.mongodb.Mongo mongo,
String databaseName,
UserCredentials userCredentials)
UserCredentialsmongo - must not be null.databaseName - must not be null or empty.userCredentials - public MongoTemplate(MongoDbFactory mongoDbFactory)
mongoDbFactory - must not be null.public MongoTemplate(MongoDbFactory mongoDbFactory, MongoConverter mongoConverter)
mongoDbFactory - must not be null.mongoConverter - public void setWriteResultChecking(WriteResultChecking resultChecking)
WriteResultChecking to be used with the template. Setting null will reset the
default of .resultChecking - public void setWriteConcern(com.mongodb.WriteConcern writeConcern)
WriteConcern to be used with the template. If none is configured the WriteConcern
configured on the MongoDbFactory will apply. If you configured a Mongo instance no
WriteConcern will be used.writeConcern - public void setWriteConcernResolver(WriteConcernResolver writeConcernResolver)
WriteConcernResolver to be used with the template.writeConcernResolver - public void setReadPreference(com.mongodb.ReadPreference readPreference)
prepareCollection(DBCollection) to set the ReadPreference before any operations are
performed.readPreference - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
setApplicationContext in interface ApplicationContextAwareBeansExceptionpublic 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
Cursor.
Returns a CloseableIterator that wraps the a Mongo DB Cursor that needs to be closed.
stream in interface MongoOperationsT - element return typequery - 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 Cursor.
Returns a CloseableIterator that wraps the a Mongo DB Cursor that needs to be closed.
stream in interface MongoOperationsT - element return typequery - must not be null.entityType - must not be null.collectionName - must not be null or empty.public String getCollectionName(Class<?> entityClass)
MongoOperationsgetCollectionName in interface MongoOperationsentityClass - must not be null.public com.mongodb.CommandResult executeCommand(String jsonCommand)
MongoOperationsexecuteCommand in interface MongoOperationsjsonCommand - a MongoDB command expressed as a JSON string.public com.mongodb.CommandResult executeCommand(com.mongodb.DBObject command)
MongoOperationsexecuteCommand in interface MongoOperationscommand - a MongoDB command@Deprecated public com.mongodb.CommandResult executeCommand(com.mongodb.DBObject command, int options)
MongoOperationsexecuteCommand in interface MongoOperationscommand - a MongoDB commandoptions - query options to usepublic com.mongodb.CommandResult executeCommand(com.mongodb.DBObject command,
com.mongodb.ReadPreference readPreference)
MongoOperationsexecuteCommand in interface MongoOperationscommand - a MongoDB command, must not be null.readPreference - read preferences to use, can be null.protected void logCommandExecutionError(com.mongodb.DBObject command,
com.mongodb.CommandResult result)
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
specificationcollectionName - name of the collection to retrieve the objects fromdch - the handler that will extract results, one document at a timeprotected void executeQuery(Query query, String collectionName, DocumentCallbackHandler dch, org.springframework.data.mongodb.core.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 fromdch - the handler that will extract results, one document at a timepreparer - allows for customization of the DBCursor 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.
Allows for returning a result object, that is a domain object or a collection of domain objects.execute in interface MongoOperationsT - return typeaction - callback object that specifies the MongoDB actions to perform on the passed in DB instance.public <T> T execute(Class<?> entityClass, CollectionCallback<T> callback)
MongoOperationsCollectionCallback 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.execute in interface MongoOperationsT - return typeentityClass - class that determines the collection to usecallback - callback object that specifies the MongoDB actionpublic <T> T execute(String collectionName, CollectionCallback<T> callback)
MongoOperationsCollectionCallback on the collection of the given name.
Allows for returning a result object, that is a domain object or a collection of domain objects.execute in interface MongoOperationsT - return typecollectionName - the name of the collection that specifies which DBCollection instance will be passed intocallback - callback object that specifies the MongoDB action the callback action.@Deprecated public <T> T executeInSession(DbCallback<T> action)
MongoOperationsDbCallback within the same connection to the database so as to ensure consistency in a
write heavy environment where you may read the data that you wrote. See the comments on Java Driver Concurrency
Allows for returning a result object, that is a domain object or a collection of domain objects.executeInSession in interface MongoOperationsT - return typeaction - callback that specified the MongoDB actions to perform on the DB instancepublic <T> com.mongodb.DBCollection createCollection(Class<T> entityClass)
MongoOperationscreateCollection in interface MongoOperationsentityClass - class that determines the collection to createpublic <T> com.mongodb.DBCollection createCollection(Class<T> entityClass, CollectionOptions collectionOptions)
MongoOperationscreateCollection in interface MongoOperationsentityClass - class that determines the collection to createcollectionOptions - options to use when creating the collection.public com.mongodb.DBCollection createCollection(String collectionName)
MongoOperationscreateCollection in interface MongoOperationscollectionName - name of the collectionpublic com.mongodb.DBCollection createCollection(String collectionName, CollectionOptions collectionOptions)
MongoOperationscreateCollection in interface MongoOperationscollectionName - name of the collectioncollectionOptions - options to use when creating the collection.public com.mongodb.DBCollection getCollection(String collectionName)
MongoOperationsgetCollection in interface MongoOperationscollectionName - name of the collectionpublic <T> boolean collectionExists(Class<T> entityClass)
MongoOperationscollectionExists in interface MongoOperationsentityClass - class that determines the name of the collectionpublic boolean collectionExists(String collectionName)
MongoOperationscollectionExists in interface MongoOperationscollectionName - name of the collectionpublic <T> void dropCollection(Class<T> entityClass)
MongoOperationsdropCollection in interface MongoOperationsentityClass - class that determines the collection to drop/delete.public void dropCollection(String collectionName)
MongoOperationsdropCollection in interface MongoOperationscollectionName - name of the collection to drop/delete.public IndexOperations indexOps(String collectionName)
MongoOperationsindexOps in interface MongoOperationspublic 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, 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.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 DB level.scriptOps in interface MongoOperationspublic <T> T findOne(Query query, Class<T> entityClass)
MongoOperationsMongoConverter. 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.findOne in interface MongoOperationsquery - the query class that specifies the criteria used to find a record and also an optional fields
specificationentityClass - the parameterized type of the returned list.public <T> T findOne(Query query, Class<T> entityClass, String collectionName)
MongoOperationsMongoConverter. 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.findOne in interface MongoOperationsquery - the query class that specifies the criteria used to find a record and also an optional fields
specificationentityClass - the parameterized type of the returned list.collectionName - name of the collection to retrieve the objects frompublic 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 parameterized 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, 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 parameterized type.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.
The query is specified as a 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
specificationentityClass - the parameterized type of the returned list.public <T> List<T> find(Query query, Class<T> entityClass, String collectionName)
MongoOperationsMongoConverter. 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.find in interface MongoOperationsquery - the query class that specifies the criteria used to find a record and also an optional fields
specificationentityClass - the parameterized type of the returned list.collectionName - name of the collection to retrieve the objects frompublic <T> T findById(Object id, Class<T> entityClass)
MongoOperationsfindById in interface MongoOperationsid - the id of the document to return.entityClass - the type the document shall be converted into.public <T> T findById(Object id, Class<T> entityClass, String collectionName)
MongoOperationsfindById in interface MongoOperationsid - the id of the document to returnentityClass - the type to convert the document tocollectionName - the collection to query for the documentpublic <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.geoNear in interface MongoOperationsnear - must not be null.entityClass - must not be null.public <T> GeoResults<T> geoNear(NearQuery near, Class<T> entityClass, 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.geoNear in interface MongoOperationsnear - 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.public <T> T findAndModify(Query query, Update 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.update - the Update to apply on matching documents.entityClass - the parameterized type.public <T> T findAndModify(Query query, Update 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.update - the Update to apply on matching documents.entityClass - the parameterized type.collectionName - the collection to query.public <T> T findAndModify(Query query, Update 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 Update to apply on matching documents.options - the FindAndModifyOptions holding additional information.entityClass - the parameterized type.public <T> T findAndModify(Query query, Update 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.update - the Update to apply on matching documents.options - the FindAndModifyOptions holding additional information.entityClass - the parameterized type.collectionName - the collection to query.public <T> T findAndRemove(Query query, Class<T> entityClass)
MongoOperationsMongoConverter.
The query is specified as a 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
specificationentityClass - the parameterized type of the returned list.public <T> T findAndRemove(Query query, Class<T> entityClass, String collectionName)
MongoOperationsMongoConverter. 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.findAndRemove in interface MongoOperationsquery - the query class that specifies the criteria used to find a record and also an optional fields
specificationentityClass - the parameterized type of the returned list.collectionName - name of the collection to retrieve the objects frompublic long count(Query query, Class<?> entityClass)
MongoOperationsQuery by querying the collection of the given entity class.count in interface MongoOperationsentityClass - 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.count in interface MongoOperationscollectionName - must not be null or empty.MongoOperations.count(Query, Class, String)public long count(Query query, Class<?> entityClass, String collectionName)
MongoOperationsQuery by querying the given collection using the given entity
class to map the given Query.count in interface MongoOperationsentityClass - must not be null.collectionName - must not be null or empty.public void insert(Object objectToSave)
MongoOperationsMongoConverter.
If you 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.insert in interface MongoOperationsobjectToSave - the object to store in the collection.public void insert(Object objectToSave, String collectionName)
MongoOperationsMongoConverter. 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.insert in interface MongoOperationsobjectToSave - the object to store in the collectioncollectionName - name of the collection to store the object inprotected void ensureNotIterable(Object o)
protected void prepareCollection(com.mongodb.DBCollection collection)
collection - protected com.mongodb.WriteConcern prepareWriteConcern(MongoAction mongoAction)
WriteConcern will be defaulted to
WriteConcern.ACKNOWLEDGED when WriteResultChecking is set to WriteResultChecking.EXCEPTION.writeConcern - any WriteConcern already configured or nullprotected <T> void doInsert(String collectionName, T objectToSave, MongoWriter<T> writer)
public void insert(Collection<? extends Object> batchToSave, Class<?> entityClass)
MongoOperationsinsert in interface MongoOperationsbatchToSave - the list of objects to save.entityClass - class that determines the collection to usepublic void insert(Collection<? extends Object> batchToSave, String collectionName)
MongoOperationsinsert in interface MongoOperationsbatchToSave - the list of objects to save.collectionName - name of the collection to store the object inpublic void insertAll(Collection<? extends Object> objectsToSave)
MongoOperationsinsertAll in interface MongoOperationsprotected <T> void doInsertAll(Collection<? extends T> listToSave, MongoWriter<T> writer)
protected <T> void doInsertBatch(String collectionName, Collection<? extends T> batchToSave, MongoWriter<T> writer)
public void save(Object objectToSave)
MongoOperationsMongoConverter. Unless
configured otherwise, an instance of MappingMongoConverter will be used.
If you 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.save in interface MongoOperationsobjectToSave - the object to store in the collectionpublic void save(Object objectToSave, String collectionName)
MongoOperationsMongoConverter. Unless
configured otherwise, an instance of MappingMongoConverter will be used.
If you 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 Cobnversion API. See Spring's
Type Conversion" for more details.save in interface MongoOperationsobjectToSave - the object to store in the collectioncollectionName - name of the collection to store the object inprotected <T> void doSave(String collectionName, T objectToSave, MongoWriter<T> writer)
protected Object insertDBObject(String collectionName, com.mongodb.DBObject dbDoc, Class<?> entityClass)
protected List<org.bson.types.ObjectId> insertDBObjectList(String collectionName, List<com.mongodb.DBObject> dbDocList)
protected Object saveDBObject(String collectionName, com.mongodb.DBObject dbDoc, Class<?> entityClass)
public com.mongodb.WriteResult upsert(Query query, Update update, Class<?> entityClass)
MongoOperationsupsert in interface MongoOperationsquery - the query document that specifies the criteria used to select a record to be upsertedupdate - the update document that contains the updated object or $ operators to manipulate the existing objectentityClass - class that determines the collection to usepublic com.mongodb.WriteResult upsert(Query query, Update update, String collectionName)
MongoOperationsMongoOperations.upsert(Query, Update, Class, String) to get full type specific support.upsert in interface MongoOperationsquery - the query document that specifies the criteria used to select a record to be updatedupdate - the update document that contains the updated object or $ operators to manipulate the existing
object.collectionName - name of the collection to update the object inpublic com.mongodb.WriteResult upsert(Query query, Update update, Class<?> entityClass, String collectionName)
MongoOperationsupsert in interface MongoOperationsquery - the query document that specifies the criteria used to select a record to be upsertedupdate - the update document that contains the updated object or $ operators to manipulate the existing objectentityClass - class of the pojo to be operated oncollectionName - name of the collection to update the object inpublic com.mongodb.WriteResult updateFirst(Query query, Update update, Class<?> entityClass)
MongoOperationsupdateFirst in interface MongoOperationsquery - the query document that specifies the criteria used to select a record to be updatedupdate - the update document that contains the updated object or $ operators to manipulate the existing
object.entityClass - class that determines the collection to usepublic com.mongodb.WriteResult updateFirst(Query query, Update update, String collectionName)
MongoOperationsMongoOperations.updateFirst(Query, Update, Class, String) to get full type specific support.updateFirst in interface MongoOperationsquery - the query document that specifies the criteria used to select a record to be updatedupdate - the update document that contains the updated object or $ operators to manipulate the existing
object.collectionName - name of the collection to update the object inpublic com.mongodb.WriteResult updateFirst(Query query, Update update, Class<?> entityClass, String collectionName)
MongoOperationsupdateFirst in interface MongoOperationsquery - the query document that specifies the criteria used to select a record to be updatedupdate - the update document that contains the updated object or $ operators to manipulate the existing
object.entityClass - class of the pojo to be operated oncollectionName - name of the collection to update the object inpublic com.mongodb.WriteResult updateMulti(Query query, Update update, Class<?> entityClass)
MongoOperationsupdateMulti in interface MongoOperationsquery - the query document that specifies the criteria used to select a record to be updatedupdate - the update document that contains the updated object or $ operators to manipulate the existing
object.entityClass - class that determines the collection to usepublic com.mongodb.WriteResult updateMulti(Query query, Update update, String collectionName)
MongoOperationsMongoOperations.updateMulti(Query, Update, 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 updatedupdate - the update document that contains the updated object or $ operators to manipulate the existing
object.collectionName - name of the collection to update the object inpublic com.mongodb.WriteResult updateMulti(Query query, Update update, Class<?> entityClass, String collectionName)
MongoOperationsupdateMulti in interface MongoOperationsquery - the query document that specifies the criteria used to select a record to be updatedupdate - the update document that contains the updated object or $ operators to manipulate the existing
object.entityClass - class of the pojo to be operated oncollectionName - name of the collection to update the object inprotected com.mongodb.WriteResult doUpdate(String collectionName, Query query, Update update, Class<?> entityClass, boolean upsert, boolean multi)
public com.mongodb.WriteResult remove(Object object)
MongoOperationsremove in interface MongoOperationspublic com.mongodb.WriteResult remove(Object object, String collection)
MongoOperationsremove in interface MongoOperationscollection - must not be null or empty.public com.mongodb.WriteResult 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 recordcollectionName - name of the collection where the objects will removedpublic com.mongodb.WriteResult remove(Query query, Class<?> entityClass)
MongoOperationsremove in interface MongoOperationspublic com.mongodb.WriteResult remove(Query query, Class<?> entityClass, String collectionName)
MongoOperationsremove in interface MongoOperationsprotected <T> com.mongodb.WriteResult doRemove(String collectionName, Query query, Class<T> entityClass)
public <T> List<T> findAll(Class<T> entityClass)
MongoOperationsMongoConverter. 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.findAll in interface MongoOperationsentityClass - the parameterized type of the returned listpublic <T> List<T> findAll(Class<T> entityClass, String collectionName)
MongoOperationsMongoConverter. 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.findAll in interface MongoOperationsentityClass - the parameterized type of the returned list.collectionName - name of the collection to retrieve the objects frompublic <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 frommapFunction - The JavaScript map functionreduceFunction - The JavaScript reduce functionentityClass - The parameterized type of the returned listpublic <T> MapReduceResults<T> mapReduce(String inputCollectionName, String mapFunction, String reduceFunction, MapReduceOptions mapReduceOptions, Class<T> entityClass)
MongoOperationsmapReduce in interface MongoOperationsinputCollectionName - the collection where the map-reduce will read frommapFunction - The JavaScript map functionreduceFunction - The JavaScript reduce functionmapReduceOptions - Options that specify detailed map-reduce behaviorentityClass - The parameterized type of the returned listpublic <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 phaseinputCollectionName - the collection where the map-reduce will read frommapFunction - The JavaScript map functionreduceFunction - The JavaScript reduce functionentityClass - The parameterized type of the returned listpublic <T> MapReduceResults<T> mapReduce(Query query, String inputCollectionName, String mapFunction, String reduceFunction, MapReduceOptions mapReduceOptions, Class<T> entityClass)
MongoOperationsmapReduce in interface MongoOperationsquery - The query to use to select the data for the map phaseinputCollectionName - the collection where the map-reduce will read frommapFunction - The JavaScript map functionreduceFunction - The JavaScript reduce functionmapReduceOptions - Options that specify detailed map-reduce behaviorentityClass - The parameterized type of the returned listpublic <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 parameterized type of the returned listpublic <T> GroupByResults<T> group(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 parameterized 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 parameterized 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 parameterized 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 parameterized 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 parameterized 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 - must not be null.collectionName - must not be null.public <T> List<T> findAllAndRemove(Query query, Class<T> entityClass)
MongoOperationsfindAllAndRemove in interface MongoOperationspublic <T> List<T> findAllAndRemove(Query query, Class<T> entityClass, String collectionName)
MongoOperationsfindAllAndRemove in interface MongoOperationsprotected <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, AggregationOperationContext context)
public Set<String> getCollectionNames()
MongoOperationsgetCollectionNames in interface MongoOperationspublic com.mongodb.DB getDb()
protected <T> void maybeEmitEvent(MongoMappingEvent<T> event)
protected com.mongodb.DBCollection doCreateCollection(String collectionName, com.mongodb.DBObject collectionOptions)
collectionName - collectionOptions - protected <T> T doFindOne(String collectionName, com.mongodb.DBObject query, com.mongodb.DBObject fields, Class<T> entityClass)
DBObject 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> List<T> doFind(String collectionName, com.mongodb.DBObject query, com.mongodb.DBObject 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, com.mongodb.DBObject query, com.mongodb.DBObject fields, Class<T> entityClass, org.springframework.data.mongodb.core.CursorPreparer preparer)
MongoConverter. The query document is
specified as a standard DBObject 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 DBCursor 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, com.mongodb.DBObject query, com.mongodb.DBObject fields, Class<S> entityClass, org.springframework.data.mongodb.core.CursorPreparer preparer, org.springframework.data.mongodb.core.MongoTemplate.DbObjectCallback<T> objectCallback)
protected com.mongodb.DBObject convertToDbObject(CollectionOptions collectionOptions)
protected <T> T doFindAndRemove(String collectionName, com.mongodb.DBObject query, com.mongodb.DBObject fields, com.mongodb.DBObject sort, 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, com.mongodb.DBObject query, com.mongodb.DBObject fields, com.mongodb.DBObject sort, Class<T> entityClass, Update update, FindAndModifyOptions options)
protected void populateIdIfNecessary(Object savedObject, Object id)
savedObject - id - protected void handleAnyWriteResultErrors(com.mongodb.WriteResult writeResult,
com.mongodb.DBObject query,
MongoActionOperation operation)
WriteResult errors based on the configured WriteResultChecking.writeResult - query - operation - Copyright © 2011–2018 Pivotal Software, Inc.. All rights reserved.