From de06029ea28f0f0abe98be6cb8c1815a75bbe58f Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 24 May 2011 17:36:00 +0200 Subject: [PATCH] Updated reference documentation for Mongo repositories. --- src/docbkx/reference/mongo-repositories.xml | 124 ++++++++++++-------- 1 file changed, 72 insertions(+), 52 deletions(-) diff --git a/src/docbkx/reference/mongo-repositories.xml b/src/docbkx/reference/mongo-repositories.xml index ed8e9a53e..f01be9132 100644 --- a/src/docbkx/reference/mongo-repositories.xml +++ b/src/docbkx/reference/mongo-repositories.xml @@ -8,7 +8,7 @@ Introduction This chapter will point out the specialties for repository support - for MongoDB. This builds on the core repository support explained in. So make sure you've got a sound understanding of the basic concepts explained there. @@ -26,12 +26,13 @@ public class Person { - private String id; - private String firstname; - private String lastname; - private Address address; + @Id + private String id; + private String firstname; + private String lastname; + private Address address; - // … getters and setters omitted + // … getters and setters omitted } @@ -47,46 +48,37 @@ Basic repository interface to persist Person entities - public interface PersonRepository extends MongoRepository<Person, Long> { + public interface PersonRepository extends PagingAndSortingRepository<Person, Long> { - // additional custom finder methods go here + // additional custom finder methods go here } - The central MongoDB CRUD repository interface is - MongoRepository. Right now this interface - simply serves typing purposes but we will add additional methods to it - later. In your Spring configuration simply add + Right now this interface simply serves typing purposes but we will + add additional methods to it later. In your Spring configuration simply + add General mongo repository Spring configuration <?xml version="1.0" encoding="UTF-8"?> - <beans xmlns="http://www.springframework.org/schema/beans" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xmlns:mongo="http://www.springframework.org/schema/data/mongo" - xmlns:context="http://www.springframework.org/schema/context" - xsi:schemaLocation="http://www.springframework.org/schema/beans - http://www.springframework.org/schema/beans/spring-beans-3.0.xsd - http://www.springframework.org/schema/data/mongo - http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd - http://www.springframework.org/schema/context - http://www.springframework.org/schema/context/spring-context-3.0.xsd"> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:mongo="http://www.springframework.org/schema/data/mongo" + xsi:schemaLocation="http://www.springframework.org/schema/beans + http://www.springframework.org/schema/beans/spring-beans-3.0.xsd + http://www.springframework.org/schema/data/mongo + http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd"> <mongo:mongo id="mongo" /> <bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate"> <constructor-arg ref="mongo" /> - <constructor-arg value="database" /> - <constructor-arg value="collection" /> - <constructor-arg> - <mongo:mapping-converter /> - </constructor-arg> + <constructor-arg value="databaseName" /> </bean> - <mongo:repositories base-package="com.acme.*.repositories" mongo-template-ref="myMongoTemplate" /> - … + <mongo:repositories base-package="com.acme.*.repositories" /> </beans> @@ -99,14 +91,13 @@ configure mongo-template-ref explicitly if you deviate from this convention. - MongoRepository extends - PagingAndSortingRepository which you can - read about in. In general it - provides you with CRUD operations as well as methods for paginated and - sorted access to the entities. Working with the repository instance is - just a matter of dependency injecting it into a client. So accessing the - second page of Persons at a page size of 10 would - simply look something like this: + As our domain repository extends + PagingAndSortingRepository it provides you + with CRUD operations as well as methods for paginated and sorted access to + the entities. Working with the repository instance is just a matter of + dependency injecting it into a client. So accessing the second page of + Persons at a page size of 10 would simply look + something like this: Paging access to Person entities @@ -145,7 +136,7 @@ public class PersonRepositoryTests { PersonRepository with query methods - public interface PersonRepository extends MongoRepository<Person, String> { + public interface PersonRepository extends PagingAndSortingRepository<Person, String> { List<Person> findByLastname(String lastname); @@ -192,8 +183,8 @@ public class PersonRepositoryTests { GreaterThan - findByAgeGreaterThan(int age) - + findByAgeGreaterThan(int + age) {"age" : {"$gt" : age}} @@ -201,8 +192,8 @@ public class PersonRepositoryTests { LessThan - findByAgeLessThan(int age) - + findByAgeLessThan(int + age) {"age" : {"$lt" : age}} @@ -210,8 +201,8 @@ public class PersonRepositoryTests { Between - findByAgeBetween(int from, int to) - + findByAgeBetween(int from, int + to) {"age" : {"$gt" : from, "$lt" : to}} @@ -237,8 +228,8 @@ public class PersonRepositoryTests { Like - findByFirstnameLike(String name) - + findByFirstnameLike(String + name) {"age" : age} ( age as regex) @@ -247,8 +238,8 @@ public class PersonRepositoryTests { (No keyword) - findByFirstname(String name) - + findByFirstname(String + name) {"age" : name} @@ -256,11 +247,40 @@ public class PersonRepositoryTests { Not - findByFirstnameNot(String name) - + findByFirstnameNot(String + name) {"age" : {"$ne" : name}} + + + Near + + findByLocationNear(Point + point) + + {"location" : {"$near" : [x,y]}} + + + + Within + + findByLocationWithin(Circle + circle) + + {"location" : {"$within" : {"$center" : [ [x, y], + distance]}}} + + + + Within + + findByLocationWithin(Box + box) + + {"location" : {"$within" : {"$box" : [ [x1, y1], + x2, y2]}}} + @@ -289,7 +309,7 @@ public class PersonRepositoryTests { public interface PersonRepository extends MongoRepository<Person, String> - @Query(value="{ 'firstname' : ?0 }", fields="{ 'firstname': 1, 'lastname': 1}") + @Query(value="{ 'firstname' : ?0 }", fields="{ 'firstname' : 1, 'lastname' : 1}") List<Person> findByThePersonsFirstname(String firstname); } @@ -389,4 +409,4 @@ Page<Person> page = repository.findAll(person.lastname.contains("a"), MongoDB queries. - + \ No newline at end of file