Spring Data Document

org.springframework.data.mongodb.core
Interface MongoOperations

All Known Implementing Classes:
MongoTemplate

public interface MongoOperations

Interface that specifies a basic set of MongoDB operations. Implemented by MongoTemplate. Not often used but a useful option for extensibility and testability (as it can be easily mocked, stubbed, or be the target of a JDK proxy).

Author:
Thomas Risberg, Mark Pollack, Oliver Gierke

Method Summary
<T> boolean
collectionExists(Class<T> entityClass)
          Check to see if a collection with a name indicated by the entity class exists.
 boolean collectionExists(String collectionName)
          Check to see if a collection with a given name exists.
 long count(Query query, Class<?> entityClass)
          Returns the number of documents for the given Query by querying the collection of the given entity class.
 long count(Query query, String collectionName)
          Returns the number of documents for the given Query querying the given collection.
<T> DBCollection
createCollection(Class<T> entityClass)
          Create an uncapped collection with a name based on the provided entity class.
<T> DBCollection
createCollection(Class<T> entityClass, CollectionOptions collectionOptions)
          Create a collect with a name based on the provided entity class using the options.
 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.
<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.
<T> T
execute(Class<?> entityClass, CollectionCallback<T> action)
          Executes the given CollectionCallback on the entity collection of the specified class.
<T> T
execute(DbCallback<T> action)
          Executes a DbCallback translating any exceptions as necessary.
<T> T
execute(String collectionName, CollectionCallback<T> action)
          Executes the given CollectionCallback on the collection of the given name.
 CommandResult executeCommand(DBObject command)
          Execute a MongoDB command.
 CommandResult executeCommand(DBObject command, int options)
          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.
 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.
<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> 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)
           
<T> T
findAndModify(Query query, Update update, FindAndModifyOptions options, Class<T> entityClass, String collectionName)
           
<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 GeoResult for all entities matching the given NearQuery.
<T> GeoResults<T>
geoNear(NearQuery near, Class<T> entityClass, String collectionName)
          Returns GeoResult for all entities matching the given NearQuery.
 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 underlying MongoConverter.
<T> GroupByResults<T>
group(Criteria criteria, String inputCollectionName, GroupBy groupBy, Class<T> entityClass)
          Execute a group operation restricting the rows to those which match the provided Criteria.
<T> GroupByResults<T>
group(String inputCollectionName, GroupBy groupBy, Class<T> entityClass)
          Execute a group operation over the entire collection.
 IndexOperations indexOps(Class<?> entityClass)
          Returns the operations that can be performed on indexes
 IndexOperations indexOps(String collectionName)
          Returns the operations that can be performed on indexes
 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.
<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.
 void remove(Object object)
          Remove the given object from the collection by id.
 void remove(Object object, String collection)
          Removes the given object from the given collection.
<T> void
remove(Query query, Class<T> entityClass)
          Remove all documents that match the provided query document criteria from the the collection used to store the entityClass.
 void remove(Query query, String collectionName)
          Remove all documents from the specified collection that match the provided query document criteria.
 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.
 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.
 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.
 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.
 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.
 WriteResult upsert(Query query, Update update, Class<?> entityClass)
          Performs an upsert.
 WriteResult upsert(Query query, Update update, String collectionName)
          Performs an upsert.
 

Method Detail

getCollectionName

String getCollectionName(Class<?> entityClass)
The collection name used for the specified class by this template.

Parameters:
entityClass - must not be null.
Returns:

executeCommand

CommandResult executeCommand(String jsonCommand)
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.

Parameters:
jsonCommand - a MongoDB command expressed as a JSON string.

executeCommand

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

Parameters:
command - a MongoDB command

executeCommand

CommandResult executeCommand(DBObject command,
                             int options)
Execute a MongoDB command. Any errors that result from executing this command will be converted into Spring's DAO exception hierarchy.

Parameters:
command - a MongoDB command
options - query options to use

executeQuery

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.

Parameters:
query - the query class that specifies the criteria used to find a record and also an optional fields specification
collectionName - name of the collection to retrieve the objects from
dch - the handler that will extract results, one document at a time

execute

<T> T execute(DbCallback<T> action)
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.

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

<T> T execute(Class<?> entityClass,
              CollectionCallback<T> action)
Executes the given CollectionCallback on the entity collection of the specified class.

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

Type Parameters:
T - return type
Parameters:
entityClass - class that determines the collection to use
action - callback object that specifies the MongoDB action
Returns:
a result object returned by the action or null

execute

<T> T execute(String collectionName,
              CollectionCallback<T> action)
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.

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

executeInSession

<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. See the comments on Java Driver Concurrency

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

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

<T> DBCollection createCollection(Class<T> entityClass)
Create an uncapped collection with a name based on the provided entity class.

Parameters:
entityClass - class that determines the collection to create
Returns:
the created collection

createCollection

<T> DBCollection createCollection(Class<T> entityClass,
                                  CollectionOptions collectionOptions)
Create a collect with a name based on the provided entity class using the options.

Parameters:
entityClass - class that determines the collection to create
collectionOptions - options to use when creating the collection.
Returns:
the created collection

createCollection

DBCollection createCollection(String collectionName)
Create an uncapped collection with the provided name.

Parameters:
collectionName - name of the collection
Returns:
the created collection

createCollection

DBCollection createCollection(String collectionName,
                              CollectionOptions collectionOptions)
Create a collect with the provided name and options.

Parameters:
collectionName - name of the collection
collectionOptions - options to use when creating the collection.
Returns:
the created collection

getCollectionNames

Set<String> getCollectionNames()
A set of collection names.

Returns:
list of collection names

getCollection

DBCollection getCollection(String collectionName)
Get a collection by name, creating it if it doesn't exist.

Translate any exceptions as necessary.

Parameters:
collectionName - name of the collection
Returns:
an existing collection or a newly created one.

collectionExists

<T> boolean collectionExists(Class<T> entityClass)
Check to see if a collection with a name indicated by the entity class exists.

Translate any exceptions as necessary.

Parameters:
entityClass - class that determines the name of the collection
Returns:
true if a collection with the given name is found, false otherwise.

collectionExists

boolean collectionExists(String collectionName)
Check to see if a collection with a given name exists.

Translate any exceptions as necessary.

Parameters:
collectionName - name of the collection
Returns:
true if a collection with the given name is found, false otherwise.

dropCollection

<T> void dropCollection(Class<T> entityClass)
Drop the collection with the name indicated by the entity class.

Translate any exceptions as necessary.

Parameters:
entityClass - class that determines the collection to drop/delete.

dropCollection

void dropCollection(String collectionName)
Drop the collection with the given name.

Translate any exceptions as necessary.

Parameters:
collectionName - name of the collection to drop/delete.

indexOps

IndexOperations indexOps(String collectionName)
Returns the operations that can be performed on indexes

Returns:
index operations on the named collection

indexOps

IndexOperations indexOps(Class<?> entityClass)
Returns the operations that can be performed on indexes

Returns:
index operations on the named collection associated with the given entity class

findAll

<T> List<T> findAll(Class<T> entityClass)
Query for a list of objects of type T from the collection used by the entity class.

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.

Parameters:
entityClass - the parameterized type of the returned list
Returns:
the converted collection

findAll

<T> List<T> findAll(Class<T> entityClass,
                    String collectionName)
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.

Parameters:
entityClass - the parameterized type of the returned list.
collectionName - name of the collection to retrieve the objects from
Returns:
the converted collection

group

<T> GroupByResults<T> group(String inputCollectionName,
                            GroupBy groupBy,
                            Class<T> entityClass)
Execute a group operation over the entire collection. The group operation entity class should match the 'shape' of the returned object that takes int account the initial document structure as well as any finalize functions.

Parameters:
criteria - The criteria that restricts the row that are considered for grouping. If not specified all rows are considered.
inputCollectionName - the collection where the group operation will read from
groupBy - the conditions under which the group operation will be performed, e.g. keys, initial document, reduce function.
entityClass - The parameterized type of the returned list
Returns:
The results of the group operation

group

<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. The group operation entity class should match the 'shape' of the returned object that takes int account the initial document structure as well as any finalize functions.

Parameters:
criteria - The criteria that restricts the row that are considered for grouping. If not specified all rows are considered.
inputCollectionName - the collection where the group operation will read from
groupBy - the conditions under which the group operation will be performed, e.g. keys, initial document, reduce function.
entityClass - The parameterized type of the returned list
Returns:
The results of the group operation

mapReduce

<T> MapReduceResults<T> mapReduce(String inputCollectionName,
                                  String mapFunction,
                                  String reduceFunction,
                                  Class<T> entityClass)
Execute a map-reduce operation. The map-reduce operation will be formed with an output type of INLINE

Parameters:
inputCollectionName - the collection where the map-reduce will read from
mapFunction - The JavaScript map function
reduceFunction - The JavaScript reduce function
mapReduceOptions - Options that specify detailed map-reduce behavior
entityClass - The parameterized type of the returned list
Returns:
The results of the map reduce operation

mapReduce

<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.

Parameters:
inputCollectionName - the collection where the map-reduce will read from
mapFunction - The JavaScript map function
reduceFunction - The JavaScript reduce function
mapReduceOptions - Options that specify detailed map-reduce behavior
entityClass - The parameterized type of the returned list
Returns:
The results of the map reduce operation

mapReduce

<T> MapReduceResults<T> mapReduce(Query query,
                                  String inputCollectionName,
                                  String mapFunction,
                                  String reduceFunction,
                                  Class<T> entityClass)
Execute a map-reduce operation that takes a query. The map-reduce operation will be formed with an output type of INLINE

Parameters:
query - The query to use to select the data for the map phase
inputCollectionName - the collection where the map-reduce will read from
mapFunction - The JavaScript map function
reduceFunction - The JavaScript reduce function
mapReduceOptions - Options that specify detailed map-reduce behavior
entityClass - The parameterized type of the returned list
Returns:
The results of the map reduce operation

mapReduce

<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

Parameters:
query - The query to use to select the data for the map phase
inputCollectionName - the collection where the map-reduce will read from
mapFunction - The JavaScript map function
reduceFunction - The JavaScript reduce function
mapReduceOptions - Options that specify detailed map-reduce behavior
entityClass - The parameterized type of the returned list
Returns:
The results of the map reduce operation

geoNear

<T> GeoResults<T> geoNear(NearQuery near,
                          Class<T> entityClass)
Returns GeoResult for all entities matching the given NearQuery. Will consider entity mapping information to determine the collection the query is ran against.

Parameters:
near - must not be null.
entityClass - must not be null.
Returns:

geoNear

<T> GeoResults<T> geoNear(NearQuery near,
                          Class<T> entityClass,
                          String collectionName)
Returns GeoResult for all entities matching the given NearQuery.

Parameters:
near - must not be null.
entityClass - must not be null.
collectionName - the collection to trigger the query against. If no collection name is given the entity class will be inspected.
Returns:

findOne

<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.

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.

Parameters:
query - the query class that specifies the criteria used to find a record and also an optional fields specification
entityClass - the parameterized type of the returned list.
Returns:
the converted object

findOne

<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.

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.

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

find

<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.

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.

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

find

<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.

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.

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

findById

<T> T findById(Object id,
               Class<T> entityClass)
Returns a document with the given id mapped onto the given class. The collection the query is ran against will be derived from the given target class as well.

Type Parameters:
T -
Parameters:
id - the id of the document to return.
entityClass - the type the document shall be converted into.
Returns:
the document with the given id mapped onto the given target class.

findById

<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.

Type Parameters:
T -
Parameters:
id - the id of the document to return
entityClass - the type to convert the document to
collectionName - the collection to query for the document
Returns:

findAndModify

<T> T findAndModify(Query query,
                    Update update,
                    Class<T> entityClass)

findAndModify

<T> T findAndModify(Query query,
                    Update update,
                    Class<T> entityClass,
                    String collectionName)

findAndModify

<T> T findAndModify(Query query,
                    Update update,
                    FindAndModifyOptions options,
                    Class<T> entityClass)

findAndModify

<T> T findAndModify(Query query,
                    Update update,
                    FindAndModifyOptions options,
                    Class<T> entityClass,
                    String collectionName)

findAndRemove

<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. 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.

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

Parameters:
query - the query class that specifies the criteria used to find a record and also an optional fields specification
entityClass - the parameterized type of the returned list.
Returns:
the converted object

findAndRemove

<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. 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.

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

count

long count(Query query,
           Class<?> entityClass)
Returns the number of documents for the given Query by querying the collection of the given entity class.

Parameters:
query -
entityClass - must not be null.
Returns:

count

long count(Query query,
           String collectionName)
Returns the number of documents for the given Query querying the given collection.

Parameters:
query -
collectionName - must not be null or empty.
Returns:

insert

void insert(Object objectToSave)
Insert the object into the collection for the entity type of the object to save.

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

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.

Parameters:
objectToSave - the object to store in the collection.

insert

void insert(Object objectToSave,
            String collectionName)
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.

Parameters:
objectToSave - the object to store in the collection
collectionName - name of the collection to store the object in

insert

void insert(Collection<? extends Object> batchToSave,
            Class<?> entityClass)
Insert a Collection of objects into a collection in a single batch write to the database.

Parameters:
batchToSave - the list of objects to save.
entityClass - class that determines the collection to use

insert

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.

Parameters:
batchToSave - the list of objects to save.
collectionName - name of the collection to store the object in

insertAll

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.

Parameters:
collectionToSave - the list of objects to save.

save

void save(Object objectToSave)
Save the object to the collection for the entity type of the object to save. 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.

Parameters:
objectToSave - the object to store in the collection

save

void save(Object objectToSave,
          String collectionName)
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.

Parameters:
objectToSave - the object to store in the collection
collectionName - name of the collection to store the object in

upsert

WriteResult upsert(Query query,
                   Update update,
                   Class<?> entityClass)
Performs an upsert. If no document is found that matches the query, a new document is created and inserted by combining the query document and the update document.

Parameters:
query - the query document that specifies the criteria used to select a record to be upserted
update - the update document that contains the updated object or $ operators to manipulate the existing object
entityClass - class that determines the collection to use
Returns:
the WriteResult which lets you access the results of the previous write.

upsert

WriteResult upsert(Query query,
                   Update update,
                   String collectionName)
Performs an upsert. If no document is found that matches the query, a new document is created and inserted by combining the query document and the update document.

Parameters:
query - the query document that specifies the criteria used to select a record to be updated
update - the update document that contains the updated object or $ operators to manipulate the existing object.
collectionName - name of the collection to update the object in
Returns:
the WriteResult which lets you access the results of the previous write.

updateFirst

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.

Parameters:
query - the query document that specifies the criteria used to select a record to be updated
update - the update document that contains the updated object or $ operators to manipulate the existing object.
entityClass - class that determines the collection to use
Returns:
the WriteResult which lets you access the results of the previous write.

updateFirst

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.

Parameters:
query - the query document that specifies the criteria used to select a record to be updated
update - the update document that contains the updated object or $ operators to manipulate the existing object.
collectionName - name of the collection to update the object in
Returns:
the WriteResult which lets you access the results of the previous write.

updateMulti

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.

Parameters:
query - the query document that specifies the criteria used to select a record to be updated
update - the update document that contains the updated object or $ operators to manipulate the existing object.
entityClass - class that determines the collection to use
Returns:
the WriteResult which lets you access the results of the previous write.

updateMulti

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.

Parameters:
query - the query document that specifies the criteria used to select a record to be updated
update - the update document that contains the updated object or $ operators to manipulate the existing object.
collectionName - name of the collection to update the object in
Returns:
the WriteResult which lets you access the results of the previous write.

remove

void remove(Object object)
Remove the given object from the collection by id.

Parameters:
object -

remove

void remove(Object object,
            String collection)
Removes the given object from the given collection.

Parameters:
object -
collection - must not be null or empty.

remove

<T> void remove(Query query,
                Class<T> entityClass)
Remove all documents that match the provided query document criteria from the the collection used to store the entityClass. The Class parameter is also used to help convert the Id of the object if it is present in the query.

Type Parameters:
T -
Parameters:
query -
entityClass -

remove

void remove(Query query,
            String collectionName)
Remove all documents from the specified collection that match the provided query document criteria. There is no conversion/mapping done for any criteria using the id field.

Parameters:
query - the query document that specifies the criteria used to remove a record
collectionName - name of the collection where the objects will removed

getConverter

MongoConverter getConverter()
Returns the underlying MongoConverter.

Returns:

Spring Data Document

Copyright © 2012. All Rights Reserved.