diff --git a/src/docbkx/reference/mongodb.xml b/src/docbkx/reference/mongodb.xml index fac9f1d9f..77e573fdc 100644 --- a/src/docbkx/reference/mongodb.xml +++ b/src/docbkx/reference/mongodb.xml @@ -83,6 +83,231 @@ Out of the box, MongoTemplate uses a Java-based default converter for most of its operations... + + +
+ Repositories + + To access domain entities stored in a MongoDB you can leverage our + sophisticated repository support that eases implementing those quite + significantly. To do so, simply create an interface for your + repository: + + + Sample Person entity + + public class Person { + + private String firstname; + private String lastname; +} + + + + Basic repository interface to persist Person entities + + public interface PersonRepository extends MongoRepository<Person, Long> { + +} + + + 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 + + <mongo:repositories base-package="com.acme.*.repositories" + mongo-template-ref="myMongoTemplate" /> + + + This namespace element will cause the base packages to be scanned + for interfaces extending MongoRepository + and create Spring beans for each of them found. These Spring beans are + backed by a generic repository implementation that provides you a variety + of useful methods to work with Person + entities. + + + Generic repository interface + + public interface Repository<T, ID extends Serializable> { + + T findById(ID id); + + List<T> findAll(); + + T save(T entity); + + List<T> findAll(Sort sort); + + Page<T> findAll(Pageable pageable); + + // further methods omitted +} + + + We've just listed a brief excerpt of the methods here. As you can + see you get access to basic CRUD operations as well as some more + sophisicated ones that allow programmatic sorting and pagination over the + entities handled by the repository. 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 + + @RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +class PersonRepositoryTests { + + @Autowired PersonRepository repository; + + @Test + public void readsFirstPageCorrectly() { + + Page<Person> persons = repository.findAll(new PageRequest(0, 10)); + assertThat(persons.isFirstPage(), is(true)); + } +} + + + The sample creates an application context with Spring's unit test + support which will perform annotation based dependency injection into test + cases. Inside the test method we simply use the repository to query the + datastore. We hand the repository a PageRequest + instance that requests the first page of persons at a page size of + 10. + +
+ Query methods + + Most of the data access operations you usually trigger on a + repository result a query being executed against the Mongo databases. + Defining such a query is just a matter of declaring a method on the + repository interface + + + PersonRepository with query methods + + public interface PersonRepository extends MongoRepository<Person, Long> { + + List<Person> findByLastname(String lastname); + + Page<Person> findByFirstname(String firstname, Pageable pageable); +} + + + The first method shows a query for all people with the given + lastname. The query will be derived parsing the method name for + constraints which can be concatenated with And and + Or. Thus the method name will result in a query + expression of {"lastname" : lastname}. The second example + shows how pagination is applied to a query. Just equip your method + signature with a Pageable parameter and + let the method return a Page instance and + we will automatically page the query accordingly. + + + Supported keywords for query methods + + + + + + + + + + + Keyword + + Sample + + Logical result + + + + + + GreaterThan + + findByAgeGreaterThan(int + age) + + {"age" : {"$gt" : age}} + + + + LessThan + + findByAgeLessThan(int + age) + + {"age" : {"$lt" : age}} + + + + Between + + findByAgeBetween(int from, int + to) + + {"age" : {"$gt" : from, "$lt" : + to}} + + + + IsNotNull, + NotNull + + findByFirstnameNotNull() + + {"age" : {"$ne" : null}} + + + + IsNull, + Null + + findByFirstnameNull() + + {"age" : null} + + + + Like + + findByFirstnameLike(String + name) + + {"age" : age} (age as + regex) + + + + (No keyword) + + findByFirstname(String + name) + + {"age" : name} + + + + Not + + findByFirstnameNot(String + name) + + {"age" : {"$ne" : name}} + + + +
+