org.springframework.data.document.mongodb
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
 boolean collectionExists(java.lang.String collectionName)
          Check to see if a collection with a given name exists.
 com.mongodb.DBCollection createCollection(java.lang.String collectionName)
          Create an uncapped collection with the provided name.
 com.mongodb.DBCollection createCollection(java.lang.String collectionName, CollectionOptions collectionOptions)
          Create a collect with the provided name and options.
 void dropCollection(java.lang.String collectionName)
          Drop the collection with the given name.
 void ensureIndex(java.lang.Class<?> entityClass, IndexDefinition indexDefinition)
          Ensure that an index for the provided IndexDefinition exists for the default collection.
 void ensureIndex(java.lang.String collectionName, IndexDefinition indexDefinition)
          Ensure that an index for the provided IndexDefinition exists.
<T> T
execute(java.lang.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(java.lang.String collectionName, CollectionCallback<T> action)
          Executes the given CollectionCallback on the collection of the given name.
 com.mongodb.CommandResult executeCommand(com.mongodb.DBObject command)
          Execute a MongoDB command.
 com.mongodb.CommandResult executeCommand(java.lang.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> java.util.List<T>
find(Query query, java.lang.Class<T> targetClass)
          Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.
<T> java.util.List<T>
find(java.lang.String collectionName, Query query, java.lang.Class<T> targetClass)
          Map the results of an ad-hoc query on the specified collection to a List of the specified type.
<T> java.util.List<T>
find(java.lang.String collectionName, Query query, java.lang.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> T
findAndRemove(Query query, java.lang.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(java.lang.String collectionName, Query query, java.lang.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
findById(java.lang.Object id, java.lang.Class<T> targetClass)
          Returns a document with the given id mapped onto the given class.
<T> T
findById(java.lang.String collectionName, java.lang.Object id, java.lang.Class<T> targetClass)
          Returns the document with the given id from the given collection mapped onto the given target class.
<T> T
findOne(Query query, java.lang.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(java.lang.String collectionName, Query query, java.lang.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> java.util.List<T>
getCollection(java.lang.Class<T> targetClass)
          Query for a list of objects of type T from the default collection.
 com.mongodb.DBCollection getCollection(java.lang.String collectionName)
          Get a collection by name, creating it if it doesn't exist.
<T> java.util.List<T>
getCollection(java.lang.String collectionName, java.lang.Class<T> targetClass)
          Query for a list of objects of type T from the specified collection.
 java.lang.String getCollectionName(java.lang.Class<?> clazz)
          The collection name used for the specified class by this template.
 java.util.Set<java.lang.String> getCollectionNames()
          A set of collection names.
 void insert(java.lang.Object objectToSave)
          Insert the object into the default collection.
 void insert(java.lang.String collectionName, java.lang.Object objectToSave)
          Insert the object into the specified collection.
 void insertList(java.util.List<? extends java.lang.Object> listToSave)
          Insert a list of objects into the default collection in a single batch write to the database.
 void insertList(java.lang.String collectionName, java.util.List<? extends java.lang.Object> listToSave)
          Insert a list of objects into the specified collection in a single batch write to the database.
 void remove(java.lang.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, java.lang.Class<T> targetClass)
          Remove all documents from the default collection that match the provided query document criteria.
 void remove(java.lang.String collectionName, Query query)
          Remove all documents from the specified collection that match the provided query document criteria.
<T> void
remove(java.lang.String collectionName, Query query, java.lang.Class<T> targetClass)
          Remove all documents from the specified collection that match the provided query document criteria.
 void save(java.lang.Object objectToSave)
          Save the object to the default collection.
 void save(java.lang.String collectionName, java.lang.Object objectToSave)
          Save the object to the specified collection.
 com.mongodb.WriteResult updateFirst(java.lang.Class<?> entityClass, 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.
 com.mongodb.WriteResult updateFirst(java.lang.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.
 com.mongodb.WriteResult updateMulti(java.lang.Class<?> entityClass, 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.
 com.mongodb.WriteResult updateMulti(java.lang.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.
 

Method Detail

getCollectionName

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

Returns:

executeCommand

com.mongodb.CommandResult executeCommand(java.lang.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

com.mongodb.CommandResult executeCommand(com.mongodb.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

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(java.lang.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(java.lang.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

com.mongodb.DBCollection createCollection(java.lang.String collectionName)
Create an uncapped collection with the provided name.

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

createCollection

com.mongodb.DBCollection createCollection(java.lang.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

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

Returns:
list of collection names

getCollection

com.mongodb.DBCollection getCollection(java.lang.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

boolean collectionExists(java.lang.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

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

Translate any exceptions as necessary.

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

getCollection

<T> java.util.List<T> getCollection(java.lang.Class<T> targetClass)
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.

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

getCollection

<T> java.util.List<T> getCollection(java.lang.String collectionName,
                                    java.lang.Class<T> targetClass)
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:
collectionName - name of the collection to retrieve the objects from
targetClass - the parameterized type of the returned list.
Returns:
the converted collection

ensureIndex

void ensureIndex(java.lang.Class<?> entityClass,
                 IndexDefinition indexDefinition)
Ensure that an index for the provided IndexDefinition exists for the default collection. If not it will be created.

Parameters:
entityClass - class that determines the collection to use
indexDefinition -

ensureIndex

void ensureIndex(java.lang.String collectionName,
                 IndexDefinition indexDefinition)
Ensure that an index for the provided IndexDefinition exists. If not it will be created.

Parameters:
collectionName -
index -

findOne

<T> T findOne(Query query,
              java.lang.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.

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
targetClass - the parameterized type of the returned list.
Returns:
the converted object

findOne

<T> T findOne(java.lang.String collectionName,
              Query query,
              java.lang.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.

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

find

<T> java.util.List<T> find(Query query,
                           java.lang.Class<T> targetClass)
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.

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

<T> java.util.List<T> find(java.lang.String collectionName,
                           Query query,
                           java.lang.Class<T> targetClass)
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:
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

<T> java.util.List<T> find(java.lang.String collectionName,
                           Query query,
                           java.lang.Class<T> targetClass,
                           CursorPreparer preparer)
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:
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.

findById

<T> T findById(java.lang.Object id,
               java.lang.Class<T> targetClass)
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.
targetClass - 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(java.lang.String collectionName,
               java.lang.Object id,
               java.lang.Class<T> targetClass)
Returns the document with the given id from the given collection mapped onto the given target class.

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

findAndRemove

<T> T findAndRemove(Query query,
                    java.lang.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. 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
targetClass - the parameterized type of the returned list.
Returns:
the converted object

findAndRemove

<T> T findAndRemove(java.lang.String collectionName,
                    Query query,
                    java.lang.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. 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:
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

insert

void insert(java.lang.Object objectToSave)
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.

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

insert

void insert(java.lang.String collectionName,
            java.lang.Object objectToSave)
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:
collectionName - name of the collection to store the object in
objectToSave - the object to store in the collection

insertList

void insertList(java.util.List<? extends java.lang.Object> listToSave)
Insert a list of objects into the default collection in a single batch write to the database.

Parameters:
listToSave - the list of objects to save.

insertList

void insertList(java.lang.String collectionName,
                java.util.List<? extends java.lang.Object> listToSave)
Insert a list of objects into the specified collection in a single batch write to the database.

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

save

void save(java.lang.Object objectToSave)
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.

Parameters:
objectToSave - the object to store in the collection

save

void save(java.lang.String collectionName,
          java.lang.Object objectToSave)
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:
collectionName - name of the collection to store the object in
objectToSave - the object to store in the collection

updateFirst

com.mongodb.WriteResult updateFirst(java.lang.Class<?> entityClass,
                                    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.

Parameters:
entityClass - class that determines the collection to use
queryDoc - the query document that specifies the criteria used to select a record to be updated
updateDoc - the update document that contains the updated object or $ operators to manipulate the existing object.

updateFirst

com.mongodb.WriteResult updateFirst(java.lang.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.

Parameters:
collectionName - name of the collection to update the object in
queryDoc - the query document that specifies the criteria used to select a record to be updated
updateDoc - the update document that contains the updated object or $ operators to manipulate the existing object.

updateMulti

com.mongodb.WriteResult updateMulti(java.lang.Class<?> entityClass,
                                    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.

Parameters:
entityClass - class that determines the collection to use
queryDoc - the query document that specifies the criteria used to select a record to be updated
updateDoc - the update document that contains the updated object or $ operators to manipulate the existing object.

updateMulti

com.mongodb.WriteResult updateMulti(java.lang.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.

Parameters:
collectionName - name of the collection to update the object in
queryDoc - the query document that specifies the criteria used to select a record to be updated
updateDoc - the update document that contains the updated object or $ operators to manipulate the existing object.

remove

void remove(java.lang.Object object)
Remove the given object from the collection by Id

Parameters:
object -

remove

void remove(Query query)
Remove all documents from the default collection that match the provided query document criteria.

Parameters:
queryDoc - the query document that specifies the criteria used to remove a record

remove

<T> void remove(Query query,
                java.lang.Class<T> targetClass)
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.

Type Parameters:
T -
Parameters:
query -
targetClass -

remove

void remove(java.lang.String collectionName,
            Query query)
Remove all documents from the specified collection that match the provided query document criteria.

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

remove

<T> void remove(java.lang.String collectionName,
                Query query,
                java.lang.Class<T> targetClass)
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.

Parameters:
collectionName -
query -
targetClass -


Copyright © 2011. All Rights Reserved.