From bbd55fa456442d54793c1fa30d16419b5f67bcfb Mon Sep 17 00:00:00 2001 From: Thomas Risberg Date: Thu, 31 Mar 2011 09:25:19 -0400 Subject: [PATCH] DATADOC-76 added documenattion for findAndRemove --- src/docbkx/reference/mongodb.xml | 2119 +++++++++++------------------- 1 file changed, 737 insertions(+), 1382 deletions(-) diff --git a/src/docbkx/reference/mongodb.xml b/src/docbkx/reference/mongodb.xml index bbda93dd5..9ee3577ec 100644 --- a/src/docbkx/reference/mongodb.xml +++ b/src/docbkx/reference/mongodb.xml @@ -1,28 +1,22 @@ +"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd"> MongoDB support One of the document stores supported by DATADOC isMongoDB. To quote the project home - page: - MongoDB (from "humongous") is a scalable, high-performance, - open source, document-oriented database. - - Spring Data Document - provides easy configuration and access to MongoDB from a Spring application. - Offers both low-level and high-level abstraction for interacting with the - store, freeing the user from infrastructural concerns. - - + url="http://www.mongodb.org/">MongoDB. To quote the project home + page: MongoDB (from "humongous") is a scalable, high-performance, + open source, document-oriented database. Spring Data Document + provides easy configuration and access to MongoDB from a Spring application. + Offers both low-level and high-level abstraction for interacting with the + store, freeing the user from infrastructural concerns.
MongoDB Requirements DATADOC requires MongoDB 1.4 while the latest production release - (1.6.5 as of this writing) is recommended. - + (1.6.5 as of this writing) is recommended.
@@ -32,79 +26,65 @@ - + Configuration Factory - - for configuring and handling communication with MongoDB via its Java driver + - for configuring and handling communication with MongoDB via its Java driver - + Template implementation - - As with many of Spring's template classes, MongoTemplate simplifies the use of accessing the database for - common use-cases and infrastructure concerns such as exception translation. Features include integrated object - mapping between documents and domain classes and fluent DSLs for query and update operations. The chapter + - As with many of Spring's template classes, MongoTemplate simplifies the use of accessing the database for common use-cases and infrastructure concerns such as exception translation. Features include integrated object mapping between documents and domain classes and fluent DSLs for query and update operations. The chapter - + - provides additional details. + provides additional details. - + Support Classes - - that offer reusable components such as mapping support and exception translation. + - that offer reusable components such as mapping support and exception translation. For most tasks, the higher-level abstractions and support services - are the best choice. Note that at any point, one can move between layers - - for example, it's very easy to get a hold of the low level connection - (org.mongo.DB) to communicate directly with MongoDB. - + are the best choice. Note that at any point, one can move between layers - + for example, it's very easy to get a hold of the low level connection + (org.mongo.DB) to communicate directly with MongoDB.
Connecting to MongoDB One of the first tasks when using MongoDB and Spring is to create a - com.mongodb.Mongo - object using the IoC container. - There are two main ways to do this, either using Java based bean metadata - or XML based bean metadata. These are discussed in the following - sections. - + com.mongodb.Mongo object using the IoC container. + There are two main ways to do this, either using Java based bean metadata + or XML based bean metadata. These are discussed in the following sections. + For those not familiar with how to configure the Spring - container using Java based bean metadata instead of XML based metadata - see the high level introduction in the reference docs - here - - as well as the detailed documentation - here. - - - + container using Java based bean metadata instead of XML based metadata + see the high level introduction in the reference docs here as well as the detailed documentation + here. +
Using Java based metadata An example of using Java based bean metadata to register an - instance of a - com.mongodb.Mongo - is shown - below - + instance of a com.mongodb.Mongo is shown below + Registering a com.mongodb.Mongo object using Java based bean - metadata - + metadata @Configuration public class AppConfig { @@ -118,62 +98,46 @@ } - - + This approach allows you to use the standard com.mongodb.Mongo API - that you may already be used to using but also pollutes the code with - the UnknownHostException checked exception. - + that you may already be used to using but also pollutes the code with + the UnknownHostException checked exception. However, you may also register an instance of - com.mongodb.Mongo - instance with the container - using Spring'sMongoFactoryBean. As - compared to instantiating a - com.mongodb.Mongo - instance directly, the FactoryBean approach has the added advantage of - also acting as an ExceptionTranslator that can be used to translate any - Mongo exceptions to exceptions in the - SpringDataAccessException. This is part ofSpring's - DAO support features. The exception translation feature works - hand in hand with Spring's - @Repository - annotation. To enable exception translation on data access components - annotated with - @Repository - register a - PersistenceExceptionTranslationPostProcessor - ( - JavaDoc) - with the container. - + com.mongodb.Mongo instance with the container + using Spring'sMongoFactoryBean. As + compared to instantiating a com.mongodb.Mongo + instance directly, the FactoryBean approach has the added advantage of + also acting as an ExceptionTranslator that can be used to translate any + Mongo exceptions to exceptions in the + SpringDataAccessException. This is part ofSpring's + DAO support features. The exception translation feature works + hand in hand with Spring's @Repository + annotation. To enable exception translation on data access components + annotated with @Repository register a + PersistenceExceptionTranslationPostProcessor + ( + JavaDoc) with the container. While enabling exception translation can be done using Java - based bean metadata it is often done declaratively in XML using - Spring's context namespace - <context::annotation-config/>.to enable - - annotation - configuration features. - - - + based bean metadata it is often done declaratively in XML using + Spring's context namespace + <context::annotation-config/>.to enable + + annotation configuration features. + An example of a Java based bean metadata that supports exception - translation on - @Repository - annotated classes is - shown below: - + translation on @Repository annotated classes is + shown below: Registering a com.mongodb.Mongo object using Spring's - MongoFactoryBean and enabling Spring's exception translation - support - + MongoFactoryBean and enabling Spring's exception translation + support @Configuration public class AppConfig { @@ -201,19 +165,14 @@ if you prefer to use the standard MongoDB API to create a - com.mongodb.Mongo instance and have exception translation enabled on - your - @Repository - instances, simply inherit from - MongoExceptionTranslationConfig - as shown - below. - + com.mongodb.Mongo instance and have exception translation enabled on + your @Repository instances, simply inherit from + MongoExceptionTranslationConfig as shown + below. Registering a com.mongodb.Mongo object and enabling Spring's - exception translation support - + exception translation support @Configuration public class AppConfig extends MongoExceptionTranslationConfig { @@ -230,20 +189,17 @@ Using XML based metadata While you can use Spring's traditional <beans/> XML - namespace to register an instance of - com.mongodb.Mongo - with the container, the XML can - be quite verbose, does not easily support the configuration of public - instance variables used with the driver's MongoOptions class, and - constructor arguments/names are not the most effective means to - distinguish between configuration of replica sets and replica pairs. o - address these issues a XML namespace is available to simplify the - configuration of a com.mongodb.Mongo instance in XML. - + namespace to register an instance of + com.mongodb.Mongo with the container, the XML can + be quite verbose, does not easily support the configuration of public + instance variables used with the driver's MongoOptions class, and + constructor arguments/names are not the most effective means to + distinguish between configuration of replica sets and replica pairs. o + address these issues a XML namespace is available to simplify the + configuration of a com.mongodb.Mongo instance in XML. To use the Mongo namespace elements you will need to reference the - Mongo schema: - + Mongo schema: XML schema to configure MongoDB @@ -274,8 +230,7 @@ A more advanced configuration with MongoOptions is shown - below - + below XML schema to configure MongoOptinos in MongoDB @@ -295,8 +250,7 @@ - A configuration using replica sets is shown below: - + A configuration using replica sets is shown below: XML schema to configure replica sets in MongoDB <beans> @@ -307,56 +261,42 @@ </beans> - - +
Working with objects using the - <classname>MongoTemplate</classname> - - - Most users are likely to use - MongoTemplate - and its corresponding package - org.springframework.data.document.mongodb - - the - template is in fact the central class of the MongoDB module due to its - rich feature set. The template offers convenience methods and automatic - mapping between MongoDB JSON documents and your domain classes. Out of the - box, - MongoTemplate - uses a Java-based default - converter but you can also write your own converter classes to be used for - reading and storing domain objects. - + MongoTemplate + + Most users are likely to use MongoTemplate + and its corresponding package + org.springframework.data.document.mongodb - the + template is in fact the central class of the MongoDB module due to its + rich feature set. The template offers convenience methods and automatic + mapping between MongoDB JSON documents and your domain classes. Out of the + box, MongoTemplate uses a Java-based default + converter but you can also write your own converter classes to be used for + reading and storing domain objects. - Once configured, - MongoTemplate - is - thread-safe and can be reused across multiple instances. - + Once configured, MongoTemplate is + thread-safe and can be reused across multiple instances. Let's look at a couple of examples for how to work with the - MongoTemplate - in the context of the Spring - container. - + MongoTemplate in the context of the Spring + container.
Instantiating MongoTemplate In Java based configuration using the driver's com.mongodb.Mongo - object - + object Registering a com.mongodb.Mongo object and enabling Spring's - exception translation support - + exception translation support @Configuration public class AppConfig extends MongoExceptionTranslationConfig { @@ -373,13 +313,11 @@ Alternatively using MongoFactoryBean, which avoid dealing with the - checked UnknownHostException - + checked UnknownHostException Registering a com.mongodb.Mongo object and enabling Spring's - exception translation support - + exception translation support @Configuration public class AppConfig { @@ -414,88 +352,53 @@ There are several overloaded constructors of MongoTemplate. - These are - + These are - - MongoTemplate - - (Mongo mongo, String - databaseName) - - - takes the default database name to - operate against - + MongoTemplate + (Mongo mongo, String databaseName) - takes the + default database name to operate against - - MongoTemplate - - (Mongo mongo, String - databaseName, String defaultCollectionName) - - - adds the - default collection name to operate against. - + MongoTemplate + (Mongo mongo, String databaseName, String + defaultCollectionName) - adds the default collection + name to operate against. - - MongoTemplate - - (Mongo mongo, String - databaseName, String defaultCollectionName, MongoConverter - mongoConverter) - - - override with a provided - MongoConverter. Default is SimpleMongoConverter - + MongoTemplate + (Mongo mongo, String databaseName, String + defaultCollectionName, MongoConverter mongoConverter) - + override with a provided MongoConverter. Default is + SimpleMongoConverter - - MongoTemplate - - (Mongo mongo, String - databaseName, String defaultCollectionName, MongoConverter - mongoConverter, WriteConcern writeConcern, WriteResultChecking - writeResultChecking) - - - Specify a default WriteConcern - and also WriteResultChecking policy - + MongoTemplate + (Mongo mongo, String databaseName, String + defaultCollectionName, MongoConverter mongoConverter, WriteConcern + writeConcern, WriteResultChecking writeResultChecking) + - Specify a default WriteConcern and also WriteResultChecking + policy - - MongoTemplate - - (Mongo mongo, String - databaseName, String defaultCollectionName, WriteConcern - writeConcern, WriteResultChecking writeResultChecking)- - Specify a default WriteConcern and also WriteResultChecking - policy - + MongoTemplate + (Mongo mongo, String databaseName, String + defaultCollectionName, WriteConcern writeConcern, + WriteResultChecking writeResultChecking)- Specify a + default WriteConcern and also WriteResultChecking policy - - MongoTemplate - - (Mongo mongo, String - databaseName, WriteConcern writeConcern, WriteResultChecking - writeResultChecking)- Specify a default WriteConcern and - also WriteResultChecking policy - + MongoTemplate + (Mongo mongo, String databaseName, WriteConcern + writeConcern, WriteResultChecking writeResultChecking)- + Specify a default WriteConcern and also WriteResultChecking + policy @@ -506,13 +409,12 @@ WriteResultChecking Policy When in development it is very handy to either log or throw an - exception if the WriteResult returned from any MongoDB operation - contains an error. It is quite common to forget to do this during - development and then end up with an application that looks like it ran - successfully but the database was not modified according to your - expectations. Setting the WriteResultChecking is an enum with the - following values, NONE, LOG, EXCEPTION. - + exception if the WriteResult returned from any MongoDB operation + contains an error. It is quite common to forget to do this during + development and then end up with an application that looks like it ran + successfully but the database was not modified according to your + expectations. Setting the WriteResultChecking is an enum with the + following values, NONE, LOG, EXCEPTION. The default is to use a WriteResultChecking of NONE.
@@ -521,122 +423,69 @@
Overview of MongoTemplate Methods - The public methods for - MongoTemplate - are - defined by the interfaceMongoOperations. - They can be grouped into the following categories: - + The public methods for MongoTemplate are + defined by the interfaceMongoOperations. + They can be grouped into the following categories:
Methods for working with a Collection - - + - - Set<String> - getCollectionNames - - () - A - set of collection names. - - - - - - boolean - collectionExists - - (String - collectionName) - - Check to see if a collection with a - given name exists. - - - - - - 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. - - - - - - void - dropCollection - - (String - collectionName) - - Drop the collection with the given - name. - - - - - - DBCollection - getCollection - - (String - collectionName) - - Get a collection by name, creating it - if it doesn't exist. - - - - - - DBCollection - getDefaultCollection - - () - The default collection used by this template. - - - - - - String - getDefaultCollectionName - - () - The default collection name used by this template. - + Set<String> getCollectionNames () + A set of collection names. - - + + + boolean collectionExists (String + collectionName) Check to see if a collection with a + given name exists. + + + + 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. + + + + void dropCollection (String + collectionName) Drop the collection with the given + name. + + + + DBCollection getCollection (String + collectionName) Get a collection by name, creating it + if it doesn't exist. + + + + DBCollection getDefaultCollection + () The default collection used by this + template. + + + + String getDefaultCollectionName + () The default collection name used by this + template. + +
@@ -644,95 +493,52 @@
Methods for executing commands - - + - - CommandResult - executeCommand - - (DBObject - command) - - Execute a MongoDB command. - - - - - - CommandResult - executeCommand - - (String - jsonCommand) - - Execute the a MongoDB command expressed - as a JSON string. - - - - - - <T> T - execute - - (CollectionCallback<T> - action) - - Executes the given CollectionCallback on the - default collection. - - - - - - <T> T - execute - - (String collectionName, - CollectionCallback<T> action) - - Executes the given - CollectionCallback on the collection of the given name.update - using the $addToSet update modifier - - - - - - <T> T - execute - - (DbCallback<T> - action) - - Executes a DbCallback translating any - exceptions as necessary. - - - - - - <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. - + CommandResult executeCommand (DBObject + command) Execute a MongoDB command. - - + + + CommandResult executeCommand (String + jsonCommand) Execute the a MongoDB command expressed + as a JSON string. + + + + <T> T execute + (CollectionCallback<T> action) + Executes the given CollectionCallback on the default + collection. + + + + <T> T execute (String collectionName, + CollectionCallback<T> action) Executes the + given CollectionCallback on the collection of the given + name.update using the $addToSet update modifier + + + + <T> T execute (DbCallback<T> + action) Executes a DbCallback translating any + exceptions as necessary. + + + + <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. + +
@@ -740,37 +546,21 @@
Methods for creating an Index - - + - - void - ensureIndex - - (IndexDefinition - indexDefintion) - - Ensure that an index for the provided - IndexDefinition exists for the default collection. - - - - - - void - ensureIndex - - (String - collectionName, IndexDefinition indexSpecification) - - Ensure that an index for the provided IndexDefinition - exists. - + void ensureIndex + (IndexDefinition indexDefintion) + Ensure that an index for the provided IndexDefinition exists for + the default collection. - - + + + void ensureIndex + (String collectionName, IndexDefinition + indexSpecification) Ensure that an index for the + provided IndexDefinition exists. + +
@@ -778,127 +568,66 @@
Methods for inserting documents - - + - - void - insert - - (Object - objectToSave) - - Insert the object into the default - collection. - - - - - - void - insert - - (String collectionName, - Object objectToSave) - - Insert the object into the - specified collection. - - - - - - void - insertList - - (List<? extends - Object> listToSave) - - Insert a list of objects into - the default collection in a single batch write to the - database. - - - - - - 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 - insert - - (T objectToSave, - MongoWriter<T> writer) - - Insert the object into - the default collection. - - - - - - <T> void - insert - - (String collectionName, T - objectToSave, MongoWriter<T> writer) - - Insert the - object into the specified collection. - - - - - - <T> void - insertList - - (List<? extends - T> listToSave, MongoWriter<T> writer) - - Insert - a list of objects into the default collection using the provided - MongoWriter instance - - - - - - <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 - + void insert + (Object objectToSave) Insert the + object into the default collection. + + + + void insert + (String collectionName, Object + objectToSave) Insert the object into the specified + collection. + + + + void insertList + (List<? extends Object> listToSave) + Insert a list of objects into the default collection + in a single batch write to the database. + + + + 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 insert (T objectToSave, + MongoWriter<T> writer) Insert the object into + the default collection. + + + + <T> void insert (String collectionName, + T objectToSave, MongoWriter<T> writer) Insert + the object into the specified collection. - - + + + <T> void insertList (List<? extends + T> listToSave, MongoWriter<T> writer) Insert + a list of objects into the default collection using the provided + MongoWriter instance + + + + <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 + +
@@ -906,191 +635,147 @@
Methods for querying for documents - - + - - <T> List<T> - getCollection - - (Class<T> - targetClass) - - Query for a list of objects of type T - from the default collection. - - - - - - <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. - - - - - - <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> - 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> List<T> getCollection (Class<T> + targetClass) Query for a list of objects of type T + from the default collection. + + + + <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. + + + + <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> 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. The first document + that matches the query is returned and also removed from the + collection in the database. + + + + <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. The first document that matches the query is + returned and also removed from the collection in the + database. - - + + + <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. The first document that matches the query is returned and + also removed from the collection in the database. + + + + <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. The first document that matches + the query is returned and also removed from the collection in + the database. + +
@@ -1098,66 +783,35 @@
Methods for saving documents - - + - - 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 - - (T objectToSave, - MongoWriter<T> writer) - - Save the object into the - default collection using the provided writer. - - - - - - <T> void - save - - (String collectionName, T - objectToSave, MongoWriter<T> writer) - - Save the - object into the specified collection using the provided - writer. - + 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 (T objectToSave, + MongoWriter<T> writer) Save the object into the + default collection using the provided writer. - - + + + <T> void save (String collectionName, T + objectToSave, MongoWriter<T> writer) Save the + object into the specified collection using the provided + writer. + +
@@ -1165,36 +819,21 @@
Methods for removing documents - - + - - void - remove - - (Query query) - 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. - + void remove + (Query query) 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. + +
@@ -1202,71 +841,41 @@
Methods for executing updates for documents - - + - - 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. - + 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. + +
@@ -1276,9 +885,8 @@ Working with collections It's time to look at some code examples showing how to use the - MongoTemplate. First we look at creating our - first collection. - + MongoTemplate. First we look at creating our + first collection. Working with collections using the MongoTemplate @@ -1297,8 +905,7 @@ Creating an index We can create an index on a collection to improve query - performance. - + performance. Creating an index using the MongoTemplate @@ -1311,20 +918,17 @@
Saving and retrieving objects as documents in a - collection - + collection Once we have created the collection and maybe an index we can - start using the collection to store our domain objects as documents. - Note that we are using a static import for - org.springframework.data.document.mongodb.query.Criteria.where - to make the query more readable. - + start using the collection to store our domain objects as documents. + Note that we are using a static import for + org.springframework.data.document.mongodb.query.Criteria.where + to make the query more readable. Inserting and retrieving documents using the - MongoTemplate - + MongoTemplate import static org.springframework.data.document.mongodb.query.Criteria.where; @@ -1343,12 +947,11 @@ Querying documents in a collection We saw how to retrieve a single document. We can also query for a - collection of documents to be returned as domain objects in a list. - Assuming that we have a number of Person objects with name and age - stored as documents in a collection and that each person has an embedded - account document with a balance. We can now run a query using the - following code. - + collection of documents to be returned as domain objects in a list. + Assuming that we have a number of Person objects with name and age + stored as documents in a collection and that each person has an embedded + account document with a balance. We can now run a query using the + following code. Querying for documents using the MongoTemplate @@ -1363,33 +966,23 @@ - All find methods take a - Query - object as a - parameter. This object defines the criteria and options used to perform - the query. The criteria is specified using a - Criteria - object that has a static factory method - named - where - used to instantiate a new - Criteria - object. We recommend using a static - import for - org.springframework.data.document.mongodb.query.Criteria.where - to make the query more readable. - + All find methods take a Query object as a + parameter. This object defines the criteria and options used to perform + the query. The criteria is specified using a + Criteria object that has a static factory method + named where used to instantiate a new + Criteria object. We recommend using a static + import for + org.springframework.data.document.mongodb.query.Criteria.where + to make the query more readable. This query should return a list of Person objects that meet the - specified criteria. The Criteria class has the following methods that - correspond to the operators provided in MongoDB. - + specified criteria. The Criteria class has the following methods that + correspond to the operators provided in MongoDB. As you can see most methods return the - Criteria - object to provide a fluent style for the - API. - + Criteria object to provide a fluent style for the + API.
Methods for the Criteria class @@ -1397,210 +990,105 @@ - - Criteria - all - - (Object o)Creates - a criterion using the - $all - operator - - - - - - Criteria - exists - - (boolean b) - Creates a criterion using the - $exists - operator - - - - - - Criteria - gt - - (Object o)Creates a - criterion using the - $gt - operator - - - - - - Criteria - gte - - (Object o)Creates - a criterion using the - $gte - operator - - - - - - Criteria - in - - (Object... o) - Creates a criterion using the - $in - operator - - - - - - Criteria - is - - (Object o)Creates a - criterion using the - $is - operator - - - - - - Criteria - lt - - (Object o)Creates a - criterion using the - $lt - operator - - - - - - Criteria - lte - - (Object o)Creates - a criterion using the - $lte - operator - - - - - - Criteria - mod - - (Number value, Number - remainder)Creates a criterion using the - $mod - operator - - - - - - Criteria - nin - - (Object... o) - Creates a criterion using the - $nin - operator - - - - - - Criteria - not - - ()Creates a - criterion using the - $not - meta operator which - affects the clause directly following - - - - - - Criteria - regex - - (String re) - Creates a criterion using a - $regex - - - - - - Criteria - size - - (int s)Creates a - criterion using the - $size - operator - - - - - - Criteria - type - - (int t)Creates a - criterion using the - $type - operator - - - - - - void - or - - (List<Query> queries) - Creates an or query using the - $or - operator for all of the provided queries - - - + Criteria all + (Object o)Creates a criterion + using the $all operator + + + + Criteria exists + (boolean b) Creates a criterion + using the $exists operator + + + + Criteria gt + (Object o)Creates a criterion + using the $gt operator + + + + Criteria gte + (Object o)Creates a criterion + using the $gte operator + + + + Criteria in + (Object... o) Creates a criterion + using the $in operator + + + + Criteria is + (Object o)Creates a criterion + using the $is operator + + + + Criteria lt + (Object o)Creates a criterion + using the $lt operator + + + + Criteria lte + (Object o)Creates a criterion + using the $lte operator + + + + Criteria mod + (Number value, Number + remainder)Creates a criterion using the + $mod operator + + + + Criteria nin + (Object... o) Creates a criterion + using the $nin operator + + + + Criteria not + ()Creates a criterion using the + $not meta operator which affects the clause + directly following + + + + Criteria regex + (String re) Creates a criterion + using a $regex + + + + Criteria size + (int s)Creates a criterion using + the $size operator + + + + Criteria type + (int t)Creates a criterion using + the $type operator + + + + void or + (List<Query> queries) + Creates an or query using the $or + operator for all of the provided queries + +
- The - Query - class has some additional methods - used to provide options for the query. - + The Query class has some additional methods + used to provide options for the query.
Methods for the Query class @@ -1608,66 +1096,37 @@ - - Query - and - - (Criteria criteria) - used to add additional criteria to the query - + Query and + (Criteria criteria) used to add + additional criteria to the query - - Field - fields - - () - used to - define fields to be included in the query results - + Field fields + () used to define fields to be + included in the query results - - Query - limit - - (int limit) - used - to limit the size of the returned results to the provided limit - (used for paging) - + Query limit + (int limit) used to limit the + size of the returned results to the provided limit (used for + paging) - - Query - skip - - (int skip) - used to - skip the provided number of documents in the results (used for - paging) - + Query skip + (int skip) used to skip the + provided number of documents in the results (used for + paging) - - Sort - sort - - () - used to provide - sort definition for the results - + Sort sort + () used to provide sort + definition for the results - + @@ -1678,14 +1137,11 @@ Updating documents in a collection For updates we can elect to update the first document found using - updateFirst - or we can update all documents that were - found to match the query usingupdateMulti. Here is - an example of an update of all SAVINGS accounts where we are adding a - one time $50.00 bonus to the balance using the - $inc - operator. - + updateFirst or we can update all documents that were + found to match the query usingupdateMulti. Here is an + example of an update of all SAVINGS accounts where we are adding a one + time $50.00 bonus to the balance using the $inc + operator. Updating documents using the MongoTemplate @@ -1700,181 +1156,84 @@ - In addition to the - Query - discussed above we - provide the update definition using an - Update - object. The - Update - class has methods that match - the update modifiers available for MongoDB. - + In addition to the Query discussed above we + provide the update definition using an Update + object. The Update class has methods that match + the update modifiers available for MongoDB. As you can see most methods return the - Update - object to provide a fluent style for the - API. - + Update object to provide a fluent style for the + API.
Methods for the Update class - - + - - Update - addToSet - - (String key, Object - value) - - Update using the - $addToSet - update modifier - - - - - - Update - inc - - (String key, Number - inc) - - Update using the - $inc - update - modifier - - - - - - Update - pop - - (String key, Update.Position - pos) - - Update using the - $pop - update - modifier - - - - - - Update - pull - - (String key, Object - value) - - Update using the - $pull - update modifier - - - - - - Update - pullAll - - (String key, Object[] - values) - - Update using the - $pullAll - update modifier - - - - - - Update - push - - (String key, Object - value) - - Update using the - $push - update modifier - - - - - - Update - pushAll - - (String key, Object[] - values) - - Update using the - $pushAll - update modifier - - - - - - Update - rename - - (String oldName, String - newName) - - Update using the - $rename - update modifier - - - - - - Update - set - - (String key, Object - value) - - Update using the - $set - update - modifier - - - - - - Update - unset - - (String key) - Update using the - $unset - update - modifier - + Update addToSet + (String key, Object value) + Update using the $addToSet update + modifier - - + + + Update inc + (String key, Number inc) Update + using the $inc update modifier + + + + Update pop + (String key, Update.Position pos) + Update using the $pop update + modifier + + + + Update pull + (String key, Object value) + Update using the $pull update modifier + + + + Update pullAll + (String key, Object[] values) + Update using the $pullAll update + modifier + + + + Update push + (String key, Object value) + Update using the $push update modifier + + + + Update pushAll + (String key, Object[] values) + Update using the $pushAll update + modifier + + + + Update rename + (String oldName, String newName) + Update using the $rename update + modifier + + + + Update set + (String key, Object value) + Update using the $set update modifier + + + + Update unset + (String key) Update using the + $unset update modifier + +
@@ -1885,14 +1244,10 @@ Road map ahead The Spring Data Document projects MongoDB support is in its early - stages. We are interested in feedback, knowing what your use cases are, - what are the common patters you encounter so that the MongoDB module - better serves your needs. Do contact us using the channels - mentioned - - above, we are - interested in hearing from you! - + stages. We are interested in feedback, knowing what your use cases are, + what are the common patters you encounter so that the MongoDB module + better serves your needs. Do contact us using the channels mentioned above, we are + interested in hearing from you!