diff --git a/src/docbkx/reference/mongodb.xml b/src/docbkx/reference/mongodb.xml index 14ed3c8b0..ff8685472 100644 --- a/src/docbkx/reference/mongodb.xml +++ b/src/docbkx/reference/mongodb.xml @@ -103,16 +103,16 @@ Then add the following to pom.xml dependencies section. - <dependency> - <groupId>org.springframework.data</groupId> - <artifactId>spring-data-mongodb</artifactId> - <version>1.0.0.BUILD-SNAPSHOT</version> - </dependency> - <dependency> - <groupId>cglib</groupId> - <artifactId>cglib</artifactId> - <version>2.2</version> - </dependency> + <dependency> + <groupId>org.springframework.data</groupId> + <artifactId>spring-data-mongodb</artifactId> + <version>1.0.0.BUILD-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>cglib</groupId> + <artifactId>cglib</artifactId> + <version>2.2</version> + </dependency> The cglib dependency is there as we will use Spring's Java @@ -228,7 +228,7 @@ MongoApp] - <Person [id=1234, name=Joe]> There is an github repository with several examples that you can download and play - around with to get a feel for how the library works. + around with to get a feel for how the library works.
@@ -722,12 +722,12 @@ Number of people = : 0 import static org.springframework.data.document.mongodb.query.Criteria.where; import static org.springframework.data.document.mongodb.query.Criteria.query; - ... + ... - Person p = new Person("Bob", 33); - mongoTemplate.insert("MyCollection", p); + Person p = new Person("Bob", 33); + mongoTemplate.insert("MyCollection", p); - Person qp = mongoTemplate.findOne("MyCollection", query(where("age").is(33)), Person.class); + Person qp = mongoTemplate.findOne("MyCollection", query(where("age").is(33)), Person.class); @@ -793,17 +793,18 @@ import static org.springframework.data.document.mongodb.query.Criteria.query; Unless and explicit MongoWriter is passed into the save or insert - method, the the template's MongoConverter will be used. + method, the the template's MongoConverter will be used.
Saving using MongoWriter The MongoWriter interface allows you to have lower level control over the mapping of an object into a DBObject. The MongoWriter - interface is + interface is /** - * A MongoWriter is responsible for converting an object of type T to the native MongoDB representation DBObject. + * A MongoWriter is responsible for converting an object of type T to the native MongoDB + * representation DBObject. * * @param <T> the type of the object to convert to a DBObject */ @@ -885,8 +886,9 @@ import static org.springframework.data.document.mongodb.query.Update ... - WriteResult wr = mongoTemplate.updateMulti(query(where("accounts.accountType").is(Account.Type.SAVINGS)), - update.inc("accounts.$.balance", 50.00)); + WriteResult wr = mongoTemplate.updateMulti( + query(where("accounts.accountType").is(Account.Type.SAVINGS)), + update.inc("accounts.$.balance", 50.00)); @@ -1093,9 +1095,10 @@ import static org.springframework.data.document.mongodb.query.Update import static org.springframework.data.document.mongodb.query.Criteria.where; import static org.springframework.data.document.mongodb.query.Query.query; - ... + ... - List<Person> result = mongoTemplate.find(query(where("age").lt(50).and("accounts.balance").gt(1000.00d)),Person.class); + List<Person> result = mongoTemplate.find( + query(where("age").lt(50).and("accounts.balance").gt(1000.00d)),Person.class); @@ -1129,6 +1132,13 @@ import static org.springframework.data.document.mongodb.query.Query.query; using the $all operator + + Criteria elemMatch (Criteria c) + Creates a criterion using the + $elemMatch operator + + Criteria exists (boolean b) Creates a criterion @@ -1458,7 +1468,7 @@ import static org.springframework.data.document.mongodb.query.Query.query; - The MongoReader can be used + The MongoReader can be used
Reading using MongoWriter @@ -1476,9 +1486,10 @@ import static org.springframework.data.document.mongodb.query.Query.query; public interface MongoReader<T> { /** - * Ready from the native MongoDB DBObject representation to an instance of the class T. The given type has to be the - * starting point for marshalling the {@link DBObject} into it. So in case there's no real valid data inside - * {@link DBObject} for the given type, just return an empty instance of the given type. + * Ready from the native MongoDB DBObject representation to an instance of the class T. + * The given type has to be the starting point for marshalling the {@link DBObject} + * into it. So in case there's no real valid data inside {@link DBObject} for the + * given type, just return an empty instance of the given type. * * @param clazz the type of the return value * @param dbo theDBObject @@ -1548,30 +1559,38 @@ public class Venue { used. Circle circle = new Circle(-73.99171, 40.738868, 0.01); -List<Venue> venues = template.find(new Query(Criteria.where("location").withinCenter(circle)), Venue.class); +List<Venue> venues = + template.find(new Query(Criteria.where("location").withinCenter(circle)), Venue.class); To find venues within a circle using Spherical coordinates the following query can be used Circle circle = new Circle(-73.99171, 40.738868, 0.003712240453784); -List<Venue> venues = template.find(new Query(Criteria.where("location").withinCenterSphere(circle)), Venue.class); +List<Venue> venues = + template.find(new Query(Criteria.where("location").withinCenterSphere(circle)), Venue.class); To find venues within a Box the following query can be used - Box box = new Box(new Point(-73.99756, 40.73083), new Point(-73.988135, 40.741404)); //lower-left then upper-right -List<Venue> venues = template.find(new Query(Criteria.where("location").withinBox(box)), Venue.class); + //lower-left then upper-right +Box box = new Box(new Point(-73.99756, 40.73083), new Point(-73.988135, 40.741404)); +List<Venue> venues = + template.find(new Query(Criteria.where("location").withinBox(box)), Venue.class); To find venues near a Point, the following query can be used Point point = new Point(-73.99171, 40.738868); -List<Venue> venues = template.find(new Query(Criteria.where("location").near(point).maxDistance(0.01)), Venue.class); +List<Venue> venues = + template.find(new Query(Criteria.where("location").near(point).maxDistance(0.01)), Venue.class); To find venues near a Point using Spherical coordines the following query can be used Point point = new Point(-73.99171, 40.738868); -List<Venue> venues = template.find(new Query(Criteria.where("location").nearSphere(point).maxDistance(0.003712240453784)), Venue.class); +List<Venue> venues = + template.find(new Query( + Criteria.where("location").nearSphere(point).maxDistance(0.003712240453784)), + Venue.class); @@ -1901,16 +1920,16 @@ public class BeforeSaveListener<BeforeSaveEvent, Person> extends AbstractM Here is an example that uses the CollectionCallback to return information about an index. - boolean hasIndex = template.execute("geolocation", new CollectionCallback<Boolean>() { - public Boolean doInCollection(DBCollection collection) throws MongoException, DataAccessException { - List<DBObject> indexes = collection.getIndexInfo(); - for (DBObject dbo : indexes) { - if ("location_2d".equals(dbo.get("name"))) { - return true; - } - } - return false; - } - }); + boolean hasIndex = template.execute("geolocation", new CollectionCallback<Boolean>() { + public Boolean doInCollection(DBCollection collection) throws MongoException, DataAccessException { + List<DBObject> indexes = collection.getIndexInfo(); + for (DBObject dbo : indexes) { + if ("location_2d".equals(dbo.get("name"))) { + return true; + } + } + return false; + } + });