From 9ff5f6426e919e755cca3be13544c9dbacd14441 Mon Sep 17 00:00:00 2001 From: Thomas Risberg Date: Mon, 14 Feb 2011 18:53:20 -0500 Subject: [PATCH] DATADOC-30 updated documenation --- src/docbkx/reference/mongodb.xml | 330 ++++++++++++++++++++++++++++--- 1 file changed, 303 insertions(+), 27 deletions(-) diff --git a/src/docbkx/reference/mongodb.xml b/src/docbkx/reference/mongodb.xml index 462953473..768110731 100644 --- a/src/docbkx/reference/mongodb.xml +++ b/src/docbkx/reference/mongodb.xml @@ -12,14 +12,14 @@ 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.
-
+
MongoDB Support High Level View The MongoDB support provides several components: @@ -36,7 +36,7 @@ - Template implemenattion + 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 @@ -60,7 +60,7 @@ (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 @@ -77,7 +77,7 @@
- Using Java based based metadata + Using Java based metadata An example of using Java based bean metadata to register an instance of a com.mongodb.Mongo is shown @@ -193,7 +193,7 @@ public class AppConfig extends MongoExceptionTranslationConfig { 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 configuraiton of replicat sets and replica pairs. o + 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. @@ -201,7 +201,7 @@ public class AppConfig extends MongoExceptionTranslationConfig { Mongo schema: - XML schmea to configure MongoDB + XML schema to configure MongoDB <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" @@ -232,7 +232,7 @@ public class AppConfig extends MongoExceptionTranslationConfig { below - XML schmea to configure MongoOptinos in MongoDB + XML schema to configure MongoOptinos in MongoDB <beans> @@ -250,7 +250,7 @@ public class AppConfig extends MongoExceptionTranslationConfig { A configuration using replica sets is shown below: - XML schmea to configure replica sets in MongoDB + XML schema to configure replica sets in MongoDB <beans> @@ -264,7 +264,7 @@ public class AppConfig extends MongoExceptionTranslationConfig {
-
+
Working with objects using the <classname>MongoTemplate</classname> @@ -422,7 +422,7 @@ public class AppConfig {
- Overivew of MongoTemplate Methods + Overview of MongoTemplate Methods The public methods for MongoTemplate are defined by the interface MongoOperations. @@ -517,7 +517,8 @@ public class AppConfig { <T> T execute(String collectionName, CollectionCallback<T> action) Executes the given - CollectionCallback on the collection of the given name. + CollectionCallback on the collection of the given name.update + using the $addToSet update modifier @@ -680,20 +681,19 @@ public class AppConfig { <T> T findOne(java.lang.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. + role="bold">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(java.lang.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. + role="bold">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. @@ -805,7 +805,7 @@ public class AppConfig { - WriteResult WriteResult updateFirst(Query query, Update update) Updates the first object that is found in the default collection that matches the query document with the @@ -904,7 +904,7 @@ public class AppConfig {
-
+
Querying documents in a collection We saw how to retrieve a single document. We can also query for a @@ -920,25 +920,301 @@ public class AppConfig { import static org.springframework.data.document.mongodb.query.Criteria.where; ... + List<Person> result = mongoTemplate.find( new Query(where("age").lt(50)).and(where("accounts.balance").gt(1000.00d)), Person.class); + 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. + 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. + +
+ Methods for the Criteria class + + + + + 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) + + + + + 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. + +
+ Methods for the Query class + + + + + 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 + + + + 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) + + + + Sort sort() used to provide + sort definition for the results + + + + + +
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 using updateMulti. 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 + + import static org.springframework.data.document.mongodb.query.Criteria.where; + +... + + WriteResult wr = mongoTemplate.updateMulti( + new Query(where("accounts.accountType").is(Account.Type.SAVINGS)), + new Update().inc("accounts.$.balance", 50.00)); + + + + 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. + +
+ 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 + + + + +
- Roadmap ahead + 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,