Spring Data Document

org.springframework.data.document.mongodb
Class MongoTemplate

java.lang.Object
  extended by org.springframework.data.document.mongodb.MongoTemplate
All Implemented Interfaces:
InitializingBean, ApplicationEventPublisherAware, MongoOperations, org.springframework.data.mapping.context.MappingContextAware

public class MongoTemplate
extends Object
implements InitializingBean, MongoOperations, ApplicationEventPublisherAware, org.springframework.data.mapping.context.MappingContextAware

Primary implementation of MongoOperations.

Author:
Thomas Risberg, Graeme Rocher, Mark Pollack, Oliver Gierke

Constructor Summary
MongoTemplate(Mongo mongo, String databaseName)
          Constructor used for a basic template configuration
MongoTemplate(Mongo mongo, String databaseName, String defaultCollectionName)
          Constructor used for a basic template configuration with a default collection name
MongoTemplate(Mongo mongo, String databaseName, String defaultCollectionName, MongoConverter mongoConverter)
          Constructor used for a template configuration with a default collection name and a custom MongoConverter
 
Method Summary
 void afterPropertiesSet()
           
 boolean collectionExists(String collectionName)
          Check to see if a collection with a given name exists.
protected  DBObject convertToDbObject(CollectionOptions collectionOptions)
           
 DBCollection createCollection(String collectionName)
          Create an uncapped collection with the provided name.
 DBCollection createCollection(String collectionName, CollectionOptions collectionOptions)
          Create a collect with the provided name and options.
protected  DBCollection doCreateCollection(String collectionName, DBObject collectionOptions)
          Create the specified collection using the provided options
protected
<T> List<T>
doFind(String collectionName, DBObject query, DBObject fields, Class<T> targetClass, 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>
doFind(String collectionName, DBObject query, DBObject fields, Class<T> targetClass, MongoReader<T> reader)
          Map the results of an ad-hoc query on the default MongoDB collection to a List using the provided MongoReader

The query document is specified as a standard DBObject and so is the fields specification.

protected
<T> T
doFindAndRemove(String collectionName, DBObject query, DBObject fields, DBObject sort, Class<T> targetClass, MongoReader<T> reader)
          Map the results of an ad-hoc query on the default MongoDB collection to an object using the provided MongoReader The first document that matches the query is returned and also removed from the collection in the database.
protected
<T> T
doFindOne(String collectionName, DBObject query, DBObject fields, Class<T> targetClass, MongoReader<T> reader)
          Map the results of an ad-hoc query on the default MongoDB collection to an object using the provided MongoReader

The query document is specified as a standard DBObject and so is the fields specification.

 void dropCollection(String collectionName)
          Drop the collection with the given name.
 void ensureIndex(IndexDefinition indexDefinition)
          Ensure that an index for the provided IndexDefinition exists for the default collection.
 void ensureIndex(String collectionName, IndexDefinition indexDefinition)
          Ensure that an index for the provided IndexDefinition exists.
<T> T
execute(CollectionCallback<T> callback)
          Executes the given CollectionCallback on the default collection.
<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.
 CommandResult executeCommand(DBObject command)
          Execute a MongoDB command.
 CommandResult executeCommand(String jsonCommand)
          Execute the a MongoDB command expressed as a JSON string.
<T> T
executeInSession(DbCallback<T> action)
          Executes the given DbCallback 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.
<T> List<T>
find(Query query, Class<T> targetClass)
          Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.
<T> List<T>
find(Query query, Class<T> targetClass, MongoReader<T> reader)
          Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.
<T> List<T>
find(String collectionName, Query query, Class<T> targetClass)
          Map the results of an ad-hoc query on the specified collection to a List of the specified type.
<T> List<T>
find(String collectionName, Query query, Class<T> targetClass, CursorPreparer preparer)
          Map the results of an ad-hoc query on the specified collection to a List of the specified type.
<T> List<T>
find(String collectionName, Query query, Class<T> targetClass, MongoReader<T> reader)
          Map the results of an ad-hoc query on the specified collection to a List of the specified type.
<T> T
findAndRemove(Query query, Class<T> targetClass)
          Map the results of an ad-hoc query on the default MongoDB collection to a single instance of an object of the specified type.
<T> T
findAndRemove(Query query, Class<T> targetClass, MongoReader<T> reader)
          Map the results of an ad-hoc query on the default MongoDB collection to a single instance of an object of the specified type.
<T> T
findAndRemove(String collectionName, Query query, Class<T> targetClass)
          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
findAndRemove(String collectionName, Query query, Class<T> targetClass, MongoReader<T> reader)
          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
findOne(Query query, Class<T> targetClass)
          Map the results of an ad-hoc query on the default MongoDB collection to a single instance of an object of the specified type.
<T> T
findOne(Query query, Class<T> targetClass, MongoReader<T> reader)
          Map the results of an ad-hoc query on the default MongoDB collection to a single instance of an object of the specified type.
<T> T
findOne(String collectionName, Query query, Class<T> targetClass)
          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
findOne(String collectionName, Query query, Class<T> targetClass, MongoReader<T> reader)
          Map the results of an ad-hoc query on the specified collection to a single instance of an object of the specified type.
<T> List<T>
getCollection(Class<T> targetClass)
          Query for a list of objects of type T from the default collection.
 DBCollection getCollection(String collectionName)
          Get a collection by name, creating it if it doesn't exist.
<T> List<T>
getCollection(String collectionName, Class<T> targetClass)
          Query for a list of objects of type T from the specified collection.
<T> List<T>
getCollection(String collectionName, Class<T> targetClass, MongoReader<T> reader)
          Query for a list of objects of type T from the specified collection, mapping the DBObject using the provided MongoReader.
 Set<String> getCollectionNames()
          A set of collection names.
 MongoConverter getConverter()
          Returns the default MongoConverter.
 DB getDb()
           
 DBCollection getDefaultCollection()
          The default collection used by this template.
 String getDefaultCollectionName()
          The default collection name used by this template.
protected  Object getIdValue(Object object)
           
 void insert(Object objectToSave)
          Insert the object into the default collection.
 void insert(String collectionName, Object objectToSave)
          Insert the object into the specified collection.
<T> void
insert(String collectionName, T objectToSave, MongoWriter<T> writer)
          Insert the object into the specified collection.
<T> void
insert(T objectToSave, MongoWriter<T> writer)
          Insert the object into the default collection.
protected  Object insertDBObject(String collectionName, DBObject dbDoc)
           
protected  List<ObjectId> insertDBObjectList(String collectionName, List<DBObject> dbDocList)
           
 void insertList(List<? extends Object> listToSave)
          Insert a list of objects into the default collection in a single batch write to the database.
<T> void
insertList(List<? extends T> listToSave, MongoWriter<T> writer)
          Insert a list of objects into the default collection using the provided MongoWriter instance
 void insertList(String collectionName, List<? extends Object> listToSave)
          Insert a list of objects into the specified collection in a single batch write to the database.
<T> void
insertList(String collectionName, List<? extends T> listToSave, MongoWriter<T> writer)
          Insert a list of objects into the specified collection using the provided MongoWriter instance
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.
 void remove(Object object)
          Remove the given object from the collection by Id
 void remove(Query query)
          Remove all documents from the default collection that match the provided query document criteria.
<T> void
remove(Query query, Class<T> targetClass)
          Remove all documents from the default collection that match the provided query document criteria.
 void remove(String collectionName, Query query)
          Remove all documents from the specified collection that match the provided query document criteria.
<T> void
remove(String collectionName, Query query, Class<T> targetClass)
          Remove all documents from the specified collection that match the provided query document criteria.
 void save(Object objectToSave)
          Save the object to the default collection.
 void save(String collectionName, Object objectToSave)
          Save the object to the specified collection.
<T> void
save(String collectionName, T objectToSave, MongoWriter<T> writer)
          Save the object into the specified collection using the provided writer.
<T> void
save(T objectToSave, MongoWriter<T> writer)
          Save the object into the default collection using the provided writer.
protected  Object saveDBObject(String collectionName, DBObject dbDoc)
           
 void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher)
           
 void setDatabaseName(String databaseName)
          Sets the database name to be used.
 void setDefaultCollectionName(String defaultCollectionName)
          Sets the name of the default collection to be used.
 void setMappingContext(org.springframework.data.mapping.model.MappingContext mappingContext)
           
 void setMongoConverter(MongoConverter converter)
           
 void setPassword(String password)
          Sets the password to use to authenticate with the Mongo database.
 void setUsername(String username)
          Sets the username to use to connect to the Mongo database
 void setWriteConcern(WriteConcern writeConcern)
           
 void setWriteResultChecking(WriteResultChecking resultChecking)
           
protected  void substituteMappedIdIfNecessary(DBObject query)
          Substitutes the id key if it is found in he query.
protected  void substituteMappedIdIfNecessary(DBObject query, Class<?> targetClass, MongoReader<?> reader)
          Substitutes the id key if it is found in he query.
 WriteResult updateFirst(Query query, Update update)
          Updates the first object that is found in the default collection that matches the query document with the provided updated document.
 WriteResult updateFirst(String collectionName, Query query, Update update)
          Updates the first object that is found in the specified collection that matches the query document criteria with the provided updated document.
 WriteResult updateMulti(Query query, Update update)
          Updates all objects that are found in the default collection that matches the query document criteria with the provided updated document.
 WriteResult updateMulti(String collectionName, Query query, Update update)
          Updates all objects that are found in the specified collection that matches the query document criteria with the provided updated document.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MongoTemplate

public MongoTemplate(Mongo mongo,
                     String databaseName)
Constructor used for a basic template configuration

Parameters:
mongo -
databaseName -

MongoTemplate

public MongoTemplate(Mongo mongo,
                     String databaseName,
                     String defaultCollectionName)
Constructor used for a basic template configuration with a default collection name

Parameters:
mongo -
databaseName -
defaultCollectionName -

MongoTemplate

public MongoTemplate(Mongo mongo,
                     String databaseName,
                     String defaultCollectionName,
                     MongoConverter mongoConverter)
Constructor used for a template configuration with a default collection name and a custom MongoConverter

Parameters:
mongo -
databaseName -
defaultCollectionName -
mongoConverter -
Method Detail

setApplicationEventPublisher

public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher)
Specified by:
setApplicationEventPublisher in interface ApplicationEventPublisherAware

setMappingContext

public void setMappingContext(org.springframework.data.mapping.model.MappingContext mappingContext)
Specified by:
setMappingContext in interface org.springframework.data.mapping.context.MappingContextAware

setUsername

public void setUsername(String username)
Sets the username to use to connect to the Mongo database

Parameters:
username - The username to use

setPassword

public void setPassword(String password)
Sets the password to use to authenticate with the Mongo database.

Parameters:
password - The password to use

setDefaultCollectionName

public void setDefaultCollectionName(String defaultCollectionName)
Sets the name of the default collection to be used.

Parameters:
defaultCollectionName -

setDatabaseName

public void setDatabaseName(String databaseName)
Sets the database name to be used.

Parameters:
databaseName -

getConverter

public MongoConverter getConverter()
Returns the default MongoConverter.

Returns:

getDefaultCollectionName

public String getDefaultCollectionName()
Description copied from interface: MongoOperations
The default collection name used by this template.

Specified by:
getDefaultCollectionName in interface MongoOperations
Returns:

getDefaultCollection

public DBCollection getDefaultCollection()
Description copied from interface: MongoOperations
The default collection used by this template.

Specified by:
getDefaultCollection in interface MongoOperations
Returns:
The default collection used by this template

executeCommand

public CommandResult executeCommand(String jsonCommand)
Description copied from interface: MongoOperations
Execute the a MongoDB command expressed as a JSON string. This will call the method JSON.parse that is part of the MongoDB driver to convert the JSON string to a DBObject. Any errors that result from executing this command will be converted into Spring's DAO exception hierarchy.

Specified by:
executeCommand in interface MongoOperations
Parameters:
jsonCommand - a MongoDB command expressed as a JSON string.

executeCommand

public CommandResult executeCommand(DBObject command)
Description copied from interface: MongoOperations
Execute a MongoDB command. Any errors that result from executing this command will be converted into Spring's DAO exception hierarchy.

Specified by:
executeCommand in interface MongoOperations
Parameters:
command - a MongoDB command

execute

public <T> T execute(DbCallback<T> action)
Description copied from interface: MongoOperations
Executes a DbCallback translating any exceptions as necessary.

Allows for returning a result object, that is a domain object or a collection of domain objects.

Specified by:
execute in interface MongoOperations
Type Parameters:
T - return type
Parameters:
action - callback object that specifies the MongoDB actions to perform on the passed in DB instance.
Returns:
a result object returned by the action or null

execute

public <T> T execute(CollectionCallback<T> callback)
Description copied from interface: MongoOperations
Executes the given CollectionCallback on the default collection.

Allows for returning a result object, that is a domain object or a collection of domain objects.

Specified by:
execute in interface MongoOperations
Type Parameters:
T - return type
Parameters:
callback - callback object that specifies the MongoDB action
Returns:
a result object returned by the action or null

execute

public <T> T execute(String collectionName,
                     CollectionCallback<T> callback)
Description copied from interface: MongoOperations
Executes the given 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.

Specified by:
execute in interface MongoOperations
Type Parameters:
T - return type
Parameters:
collectionName - the name of the collection that specifies which DBCollection instance will be passed into
callback - callback object that specifies the MongoDB action the callback action.
Returns:
a result object returned by the action or null

executeInSession

public <T> T executeInSession(DbCallback<T> action)
Description copied from interface: MongoOperations
Executes the given DbCallback 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.

Specified by:
executeInSession in interface MongoOperations
Type Parameters:
T - return type
Parameters:
action - callback that specified the MongoDB actions to perform on the DB instance
Returns:
a result object returned by the action or null

createCollection

public DBCollection createCollection(String collectionName)
Description copied from interface: MongoOperations
Create an uncapped collection with the provided name.

Specified by:
createCollection in interface MongoOperations
Parameters:
collectionName - name of the collection
Returns:
the created collection

createCollection

public DBCollection createCollection(String collectionName,
                                     CollectionOptions collectionOptions)
Description copied from interface: MongoOperations
Create a collect with the provided name and options.

Specified by:
createCollection in interface MongoOperations
Parameters:
collectionName - name of the collection
collectionOptions - options to use when creating the collection.
Returns:
the created collection

getCollection

public DBCollection getCollection(String collectionName)
Description copied from interface: MongoOperations
Get a collection by name, creating it if it doesn't exist.

Translate any exceptions as necessary.

Specified by:
getCollection in interface MongoOperations
Parameters:
collectionName - name of the collection
Returns:
an existing collection or a newly created one.

collectionExists

public boolean collectionExists(String collectionName)
Description copied from interface: MongoOperations
Check to see if a collection with a given name exists.

Translate any exceptions as necessary.

Specified by:
collectionExists in interface MongoOperations
Parameters:
collectionName - name of the collection
Returns:
true if a collection with the given name is found, false otherwise.

dropCollection

public void dropCollection(String collectionName)
Description copied from interface: MongoOperations
Drop the collection with the given name.

Translate any exceptions as necessary.

Specified by:
dropCollection in interface MongoOperations
Parameters:
collectionName - name of the collection to drop/delete.

ensureIndex

public void ensureIndex(IndexDefinition indexDefinition)
Description copied from interface: MongoOperations
Ensure that an index for the provided IndexDefinition exists for the default collection. If not it will be created.

Specified by:
ensureIndex in interface MongoOperations

ensureIndex

public void ensureIndex(String collectionName,
                        IndexDefinition indexDefinition)
Description copied from interface: MongoOperations
Ensure that an index for the provided IndexDefinition exists. If not it will be created.

Specified by:
ensureIndex in interface MongoOperations

findOne

public <T> T findOne(Query query,
                     Class<T> targetClass)
Description copied from interface: MongoOperations
Map the results of an ad-hoc query on the default MongoDB collection to a single instance of an object of the specified type.

The object is converted from the MongoDB native representation using an instance of MongoConverter. Unless configured otherwise, an instance of SimpleMongoConverter will be used.

The query is specified as a Query which can be created either using the BasicQuery or the more feature rich Query.

Specified by:
findOne in interface MongoOperations
Parameters:
query - the query class that specifies the criteria used to find a record and also an optional fields specification
targetClass - the parameterized type of the returned list.
Returns:
the converted object

findOne

public <T> T findOne(Query query,
                     Class<T> targetClass,
                     MongoReader<T> reader)
Description copied from interface: MongoOperations
Map the results of an ad-hoc query on the default MongoDB collection to a single instance of an object of the specified type.

The object is converted from the MongoDB native representation using an instance of MongoConverter. Unless configured otherwise, an instance of SimpleMongoConverter will be used.

The query is specified as a Query which can be created either using the BasicQuery or the more feature rich Query.

Specified by:
findOne in interface MongoOperations
Parameters:
query - the query class that specifies the criteria used to find a record and also an optional fields specification
targetClass - the parameterized type of the returned list.
reader - the MongoReader to convert from DBObject to an object.
Returns:
the converted object

findOne

public <T> T findOne(String collectionName,
                     Query query,
                     Class<T> targetClass)
Description copied from interface: MongoOperations
Map the results of an ad-hoc query on the specified collection to a single instance of an object of the specified type.

The object is converted from the MongoDB native representation using an instance of MongoConverter. Unless configured otherwise, an instance of SimpleMongoConverter will be used.

The query is specified as a Query which can be created either using the BasicQuery or the more feature rich Query.

Specified by:
findOne in interface MongoOperations
Parameters:
collectionName - name of the collection to retrieve the objects from
query - the query class that specifies the criteria used to find a record and also an optional fields specification
targetClass - the parameterized type of the returned list.
Returns:
the converted object

findOne

public <T> T findOne(String collectionName,
                     Query query,
                     Class<T> targetClass,
                     MongoReader<T> reader)
Description copied from interface: MongoOperations
Map the results of an ad-hoc query on the specified collection to a single instance of an object of the specified type.

The object is converted from the MongoDB native representation using an instance of MongoConverter. Unless configured otherwise, an instance of SimpleMongoConverter will be used.

The query is specified as a Query which can be created either using the BasicQuery or the more feature rich Query.

Specified by:
findOne in interface MongoOperations
Parameters:
collectionName - name of the collection to retrieve the objects from
query - the query class that specifies the criteria used to find a record and also an optional fields specification
targetClass - the parameterized type of the returned list.
reader - the MongoReader to convert from DBObject to an object.
Returns:
the converted object

find

public <T> List<T> find(Query query,
                        Class<T> targetClass)
Description copied from interface: MongoOperations
Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.

The object is converted from the MongoDB native representation using an instance of MongoConverter. Unless configured otherwise, an instance of SimpleMongoConverter will be used.

The query is specified as a Query which can be created either using the BasicQuery or the more feature rich Query.

Specified by:
find in interface MongoOperations
Parameters:
query - the query class that specifies the criteria used to find a record and also an optional fields specification
targetClass - the parameterized type of the returned list.
Returns:
the List of converted objects

find

public <T> List<T> find(Query query,
                        Class<T> targetClass,
                        MongoReader<T> reader)
Description copied from interface: MongoOperations
Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.

The object is converted from the MongoDB native representation using an instance of MongoConverter. Unless configured otherwise, an instance of SimpleMongoConverter will be used.

The query is specified as a Query which can be created either using the BasicQuery or the more feature rich Query.

Specified by:
find in interface MongoOperations
Parameters:
query - the query class that specifies the criteria used to find a record and also an optional fields specification
targetClass - the parameterized type of the returned list.
reader - the MongoReader to convert from DBObject to an object.
Returns:
the List of converted objects

find

public <T> List<T> find(String collectionName,
                        Query query,
                        Class<T> targetClass)
Description copied from interface: MongoOperations
Map the results of an ad-hoc query on the specified collection to a List of the specified type.

The object is converted from the MongoDB native representation using an instance of MongoConverter. Unless configured otherwise, an instance of SimpleMongoConverter will be used.

The query is specified as a Query which can be created either using the BasicQuery or the more feature rich Query.

Specified by:
find in interface MongoOperations
Parameters:
collectionName - name of the collection to retrieve the objects from
query - the query class that specifies the criteria used to find a record and also an optional fields specification
targetClass - the parameterized type of the returned list.
Returns:
the List of converted objects

find

public <T> List<T> find(String collectionName,
                        Query query,
                        Class<T> targetClass,
                        MongoReader<T> reader)
Description copied from interface: MongoOperations
Map the results of an ad-hoc query on the specified collection to a List of the specified type.

The object is converted from the MongoDB native representation using an instance of MongoConverter. Unless configured otherwise, an instance of SimpleMongoConverter will be used.

The query is specified as a Query which can be created either using the BasicQuery or the more feature rich Query.

Specified by:
find in interface MongoOperations
Parameters:
collectionName - name of the collection to retrieve the objects from
query - the query class that specifies the criteria used to find a record and also an optional fields specification
targetClass - the parameterized type of the returned list.
reader - the MongoReader to convert from DBObject to an object.
Returns:
the List of converted objects

find

public <T> List<T> find(String collectionName,
                        Query query,
                        Class<T> targetClass,
                        CursorPreparer preparer)
Description copied from interface: MongoOperations
Map the results of an ad-hoc query on the specified collection to a List of the specified type.

The object is converted from the MongoDB native representation using an instance of MongoConverter. Unless configured otherwise, an instance of SimpleMongoConverter will be used.

The query is specified as a Query which can be created either using the BasicQuery or the more feature rich Query.

Specified by:
find in interface MongoOperations
Parameters:
collectionName - name of the collection to retrieve the objects from
query - the query class that specifies the criteria used to find a record and also an optional fields specification
targetClass - 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).
Returns:
the List of converted objects.

findAndRemove

public <T> T findAndRemove(Query query,
                           Class<T> targetClass)
Description copied from interface: MongoOperations
Map the results of an ad-hoc query on the default MongoDB collection to a single instance of an object of the specified type. The first document that matches the query is returned and also removed from the collection in the database.

The object is converted from the MongoDB native representation using an instance of MongoConverter. Unless configured otherwise, an instance of SimpleMongoConverter will be used.

The query is specified as a Query which can be created either using the BasicQuery or the more feature rich Query.

Specified by:
findAndRemove in interface MongoOperations
Parameters:
query - the query class that specifies the criteria used to find a record and also an optional fields specification
targetClass - the parameterized type of the returned list.
Returns:
the converted object

findAndRemove

public <T> T findAndRemove(Query query,
                           Class<T> targetClass,
                           MongoReader<T> reader)
Description copied from interface: MongoOperations
Map the results of an ad-hoc query on the default MongoDB collection to a single instance of an object of the specified type. The first document that matches the query is returned and also removed from the collection in the database.

The object is converted from the MongoDB native representation using an instance of MongoConverter. Unless configured otherwise, an instance of SimpleMongoConverter will be used.

The query is specified as a Query which can be created either using the BasicQuery or the more feature rich Query.

Specified by:
findAndRemove in interface MongoOperations
Parameters:
query - the query class that specifies the criteria used to find a record and also an optional fields specification
targetClass - the parameterized type of the returned list.
reader - the MongoReader to convert from DBObject to an object.
Returns:
the converted object

findAndRemove

public <T> T findAndRemove(String collectionName,
                           Query query,
                           Class<T> targetClass)
Description copied from interface: MongoOperations
Map the results of an ad-hoc query on the specified collection to a single instance of an object of the specified type. The first document that matches the query is returned and also removed from the collection in the database.

The object is converted from the MongoDB native representation using an instance of MongoConverter. Unless configured otherwise, an instance of SimpleMongoConverter will be used.

The query is specified as a Query which can be created either using the BasicQuery or the more feature rich Query.

Specified by:
findAndRemove in interface MongoOperations
Parameters:
collectionName - name of the collection to retrieve the objects from
query - the query class that specifies the criteria used to find a record and also an optional fields specification
targetClass - the parameterized type of the returned list.
Returns:
the converted object

findAndRemove

public <T> T findAndRemove(String collectionName,
                           Query query,
                           Class<T> targetClass,
                           MongoReader<T> reader)
Description copied from interface: MongoOperations
Map the results of an ad-hoc query on the specified collection to a single instance of an object of the specified type. The first document that matches the query is returned and also removed from the collection in the database.

The object is converted from the MongoDB native representation using an instance of MongoConverter. Unless configured otherwise, an instance of SimpleMongoConverter will be used.

The query is specified as a Query which can be created either using the BasicQuery or the more feature rich Query.

Specified by:
findAndRemove in interface MongoOperations
Parameters:
collectionName - name of the collection to retrieve the objects from
query - the query class that specifies the criteria used to find a record and also an optional fields specification
targetClass - the parameterized type of the returned list.
reader - the MongoReader to convert from DBObject to an object.
Returns:
the converted object

insert

public void insert(Object objectToSave)
Description copied from interface: MongoOperations
Insert the object into the default collection.

The object is converted to the MongoDB native representation using an instance of MongoConverter. Unless configured otherwise, an instance of SimpleMongoConverter 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 Spring 3.0's new Type Conversion API. See Spring 3 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.

Specified by:
insert in interface MongoOperations
Parameters:
objectToSave - the object to store in the collection.

insert

public void insert(String collectionName,
                   Object objectToSave)
Description copied from interface: MongoOperations
Insert the object into the specified collection.

The object is converted to the MongoDB native representation using an instance of MongoConverter. Unless configured otherwise, an instance of SimpleMongoConverter will be used.

Insert is used to initially store the object into the database. To update an existing object use the save method.

Specified by:
insert in interface MongoOperations
Parameters:
collectionName - name of the collection to store the object in
objectToSave - the object to store in the collection

insert

public <T> void insert(T objectToSave,
                       MongoWriter<T> writer)
Description copied from interface: MongoOperations
Insert the object into the default collection.

The object is converted to the MongoDB native representation using an instance of MongoWriter

Insert is used to initially store the object into the database. To update an existing object use the save method.

Specified by:
insert in interface MongoOperations
Type Parameters:
T - the type of the object to insert
Parameters:
objectToSave - the object to store in the collection
writer - the writer to convert the object to save into a DBObject

insert

public <T> void insert(String collectionName,
                       T objectToSave,
                       MongoWriter<T> writer)
Description copied from interface: MongoOperations
Insert the object into the specified collection.

The object is converted to the MongoDB native representation using an instance of MongoWriter

Insert is used to initially store the object into the database. To update an existing object use the save method.

Specified by:
insert in interface MongoOperations
Type Parameters:
T - the type of the object to insert
Parameters:
collectionName - name of the collection to store the object in
objectToSave - the object to store in the collection
writer - the writer to convert the object to save into a DBObject

insertList

public void insertList(List<? extends Object> listToSave)
Description copied from interface: MongoOperations
Insert a list of objects into the default collection in a single batch write to the database.

Specified by:
insertList in interface MongoOperations
Parameters:
listToSave - the list of objects to save.

insertList

public void insertList(String collectionName,
                       List<? extends Object> listToSave)
Description copied from interface: MongoOperations
Insert a list of objects into the specified collection in a single batch write to the database.

Specified by:
insertList in interface MongoOperations
Parameters:
collectionName - name of the collection to store the object in
listToSave - the list of objects to save.

insertList

public <T> void insertList(List<? extends T> listToSave,
                           MongoWriter<T> writer)
Description copied from interface: MongoOperations
Insert a list of objects into the default collection using the provided MongoWriter instance

Specified by:
insertList in interface MongoOperations
Type Parameters:
T - the type of object being saved
Parameters:
listToSave - the list of objects to save.
writer - the writer to convert the object to save into a DBObject

insertList

public <T> void insertList(String collectionName,
                           List<? extends T> listToSave,
                           MongoWriter<T> writer)
Description copied from interface: MongoOperations
Insert a list of objects into the specified collection using the provided MongoWriter instance

Specified by:
insertList in interface MongoOperations
Type Parameters:
T - the type of object being saved
Parameters:
collectionName - name of the collection to store the object in
listToSave - the list of objects to save.
writer - the writer to convert the object to save into a DBObject

save

public void save(Object objectToSave)
Description copied from interface: MongoOperations
Save the object to the default collection. This will perform an insert if the object is not already present, that is an 'upsert'.

The object is converted to the MongoDB native representation using an instance of MongoConverter. Unless configured otherwise, an instance of SimpleMongoConverter 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 Spring 3.0's new Type Conversion API. See Spring 3 Type Conversion" for more details.

Specified by:
save in interface MongoOperations
Parameters:
objectToSave - the object to store in the collection

save

public void save(String collectionName,
                 Object objectToSave)
Description copied from interface: MongoOperations
Save the object to the specified collection. This will perform an insert if the object is not already present, that is an 'upsert'.

The object is converted to the MongoDB native representation using an instance of MongoConverter. Unless configured otherwise, an instance of SimpleMongoConverter 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 Spring 3.0's new Type Cobnversion API. See Spring 3 Type Conversion" for more details.

Specified by:
save in interface MongoOperations
Parameters:
collectionName - name of the collection to store the object in
objectToSave - the object to store in the collection

save

public <T> void save(T objectToSave,
                     MongoWriter<T> writer)
Description copied from interface: MongoOperations
Save the object into the default collection using the provided writer. This will perform an insert if the object is not already present, that is an 'upsert'.

The object is converted to the MongoDB native representation using an instance of MongoWriter

Specified by:
save in interface MongoOperations
Type Parameters:
T - the type of the object to insert
Parameters:
objectToSave - the object to store in the collection
writer - the writer to convert the object to save into a DBObject

save

public <T> void save(String collectionName,
                     T objectToSave,
                     MongoWriter<T> writer)
Description copied from interface: MongoOperations
Save the object into the specified collection using the provided writer. This will perform an insert if the object is not already present, that is an 'upsert'.

The object is converted to the MongoDB native representation using an instance of MongoWriter

Specified by:
save in interface MongoOperations
Type Parameters:
T - the type of the object to insert
Parameters:
collectionName - name of the collection to store the object in
objectToSave - the object to store in the collection
writer - the writer to convert the object to save into a DBObject

insertDBObject

protected Object insertDBObject(String collectionName,
                                DBObject dbDoc)

insertDBObjectList

protected List<ObjectId> insertDBObjectList(String collectionName,
                                            List<DBObject> dbDocList)

saveDBObject

protected Object saveDBObject(String collectionName,
                              DBObject dbDoc)

updateFirst

public WriteResult updateFirst(Query query,
                               Update update)
Description copied from interface: MongoOperations
Updates the first object that is found in the default collection that matches the query document with the provided updated document.

Specified by:
updateFirst in interface MongoOperations

updateFirst

public WriteResult updateFirst(String collectionName,
                               Query query,
                               Update update)
Description copied from interface: MongoOperations
Updates the first object that is found in the specified collection that matches the query document criteria with the provided updated document.

Specified by:
updateFirst in interface MongoOperations
Parameters:
collectionName - name of the collection to update the object in

updateMulti

public WriteResult updateMulti(Query query,
                               Update update)
Description copied from interface: MongoOperations
Updates all objects that are found in the default collection that matches the query document criteria with the provided updated document.

Specified by:
updateMulti in interface MongoOperations

updateMulti

public WriteResult updateMulti(String collectionName,
                               Query query,
                               Update update)
Description copied from interface: MongoOperations
Updates all objects that are found in the specified collection that matches the query document criteria with the provided updated document.

Specified by:
updateMulti in interface MongoOperations
Parameters:
collectionName - name of the collection to update the object in

remove

public void remove(Query query)
Description copied from interface: MongoOperations
Remove all documents from the default collection that match the provided query document criteria.

Specified by:
remove in interface MongoOperations

remove

public void remove(Object object)
Description copied from interface: MongoOperations
Remove the given object from the collection by Id

Specified by:
remove in interface MongoOperations

remove

public <T> void remove(Query query,
                       Class<T> targetClass)
Description copied from interface: MongoOperations
Remove all documents from the default collection that match the provided query document criteria. The Class parameter is used to help convert the Id of the object if it is present in the query.

Specified by:
remove in interface MongoOperations

remove

public <T> void remove(String collectionName,
                       Query query,
                       Class<T> targetClass)
Description copied from interface: MongoOperations
Remove all documents from the specified collection that match the provided query document criteria. The Class parameter is used to help convert the Id of the object if it is present in the query.

Specified by:
remove in interface MongoOperations

remove

public void remove(String collectionName,
                   Query query)
Description copied from interface: MongoOperations
Remove all documents from the specified collection that match the provided query document criteria.

Specified by:
remove in interface MongoOperations
Parameters:
collectionName - name of the collection where the objects will removed

getCollection

public <T> List<T> getCollection(Class<T> targetClass)
Description copied from interface: MongoOperations
Query for a list of objects of type T from the default collection.

The object is converted from the MongoDB native representation using an instance of MongoConverter. Unless configured otherwise, an instance of SimpleMongoConverter 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.

Specified by:
getCollection in interface MongoOperations
Parameters:
targetClass - the parameterized type of the returned list
Returns:
the converted collection

getCollection

public <T> List<T> getCollection(String collectionName,
                                 Class<T> targetClass)
Description copied from interface: MongoOperations
Query for a list of objects of type T from the specified collection.

The object is converted from the MongoDB native representation using an instance of MongoConverter. Unless configured otherwise, an instance of SimpleMongoConverter 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.

Specified by:
getCollection in interface MongoOperations
Parameters:
collectionName - name of the collection to retrieve the objects from
targetClass - the parameterized type of the returned list.
Returns:
the converted collection

getCollectionNames

public Set<String> getCollectionNames()
Description copied from interface: MongoOperations
A set of collection names.

Specified by:
getCollectionNames in interface MongoOperations
Returns:
list of collection names

getCollection

public <T> List<T> getCollection(String collectionName,
                                 Class<T> targetClass,
                                 MongoReader<T> reader)
Description copied from interface: MongoOperations
Query for a list of objects of type T from the specified collection, mapping the DBObject using the provided MongoReader.

Specified by:
getCollection in interface MongoOperations
Parameters:
collectionName - name of the collection to retrieve the objects from
targetClass - the parameterized type of the returned list.
reader - the MongoReader to convert from DBObject to an object.
Returns:
the converted collection

getDb

public DB getDb()

maybeEmitEvent

protected <T> void maybeEmitEvent(MongoMappingEvent<T> event)

doCreateCollection

protected DBCollection doCreateCollection(String collectionName,
                                          DBObject collectionOptions)
Create the specified collection using the provided options

Parameters:
collectionName -
collectionOptions -
Returns:
the collection that was created

doFindOne

protected <T> T doFindOne(String collectionName,
                          DBObject query,
                          DBObject fields,
                          Class<T> targetClass,
                          MongoReader<T> reader)
Map the results of an ad-hoc query on the default MongoDB collection to an object using the provided MongoReader

The query document is specified as a standard DBObject and so is the fields specification.

Parameters:
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
targetClass - the parameterized type of the returned list.
reader - the MongoReader to convert from DBObject to an object.
Returns:
the List of converted objects.

doFind

protected <T> List<T> doFind(String collectionName,
                             DBObject query,
                             DBObject fields,
                             Class<T> targetClass,
                             CursorPreparer preparer)
Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.

The object is converted from the MongoDB native representation using an instance of MongoConverter. Unless configured otherwise, an instance of SimpleMongoConverter will be used.

The query document is specified as a standard DBObject and so is the fields specification.

Can be overridden by subclasses.

Parameters:
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
targetClass - 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).
Returns:
the List of converted objects.

doFind

protected <T> List<T> doFind(String collectionName,
                             DBObject query,
                             DBObject fields,
                             Class<T> targetClass,
                             MongoReader<T> reader)
Map the results of an ad-hoc query on the default MongoDB collection to a List using the provided MongoReader

The query document is specified as a standard DBObject and so is the fields specification.

Parameters:
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
targetClass - the parameterized type of the returned list.
reader - the MongoReader to convert from DBObject to an object.
Returns:
the List of converted objects.

convertToDbObject

protected DBObject convertToDbObject(CollectionOptions collectionOptions)

doFindAndRemove

protected <T> T doFindAndRemove(String collectionName,
                                DBObject query,
                                DBObject fields,
                                DBObject sort,
                                Class<T> targetClass,
                                MongoReader<T> reader)
Map the results of an ad-hoc query on the default MongoDB collection to an object using the provided MongoReader The first document that matches the query is returned and also removed from the collection in the database.

The query document is specified as a standard DBObject and so is the fields specification.

Parameters:
collectionName - name of the collection to retrieve the objects from
query - the query document that specifies the criteria used to find a record
targetClass - the parameterized type of the returned list.
reader - the MongoReader to convert from DBObject to an object.
Returns:
the List of converted objects.

getIdValue

protected Object getIdValue(Object object)

populateIdIfNecessary

protected void populateIdIfNecessary(Object savedObject,
                                     Object id)
Populates the id property of the saved object, if it's not set already.

Parameters:
savedObject -
id -

substituteMappedIdIfNecessary

protected void substituteMappedIdIfNecessary(DBObject query,
                                             Class<?> targetClass,
                                             MongoReader<?> reader)
Substitutes the id key if it is found in he query. Any 'id' keys will be replaced with '_id' and the value converted to an ObjectId if possible. This conversion should match the way that the id fields are converted during read operations.

Parameters:
query -
targetClass -
reader -

substituteMappedIdIfNecessary

protected void substituteMappedIdIfNecessary(DBObject query)
Substitutes the id key if it is found in he query. Any 'id' keys will be replaced with '_id'. No conversion of the value to an ObjectId is possible since we don't have access to a targetClass or a converter. This means the value has to be of the correct form.

Parameters:
query -

afterPropertiesSet

public void afterPropertiesSet()
Specified by:
afterPropertiesSet in interface InitializingBean

setMongoConverter

public void setMongoConverter(MongoConverter converter)

setWriteResultChecking

public void setWriteResultChecking(WriteResultChecking resultChecking)

setWriteConcern

public void setWriteConcern(WriteConcern writeConcern)

Spring Data Document

Copyright © 2011. All Rights Reserved.