@ -30,9 +30,9 @@ The jumping off ground for learning about MongoDB is http://www.mongodb.org/[www
@@ -30,9 +30,9 @@ The jumping off ground for learning about MongoDB is http://www.mongodb.org/[www
[[requirements]]
== Requirements
Spring Data MongoDB 1.x binaries requires JDK level 6.0 and above, and http://spring.io/docs[Spring Framework] 3.2.x and above.
Spring Data MongoDB 1.x binaries requires JDK level 6.0 and above, and http://spring.io/docs[Spring Framework] 4.0.x and above.
In terms of document stores, http://www.mongodb.org/[MongoDB] at least 2.4, preferably version 2.6.
In terms of document stores, http://www.mongodb.org/[MongoDB] at least 2.6.
== Additional Help Resources
@ -51,7 +51,7 @@ Spring Data on Stackoverflow http://stackoverflow.com/questions/tagged/spring-da
@@ -51,7 +51,7 @@ Spring Data on Stackoverflow http://stackoverflow.com/questions/tagged/spring-da
[[get-started:help:professional]]
==== Professional Support
Professional, from-the-source support, with guaranteed response time, is available from http://gopivotal.com/[Pivotal Sofware, Inc.], the company behind Spring Data and Spring.
Professional, from-the-source support, with guaranteed response time, is available from http://pivotal.io/[Pivotal Sofware, Inc.], the company behind Spring Data and Spring.
@ -134,16 +134,22 @@ Most of the data access operations you usually trigger on a repository result a
@@ -134,16 +134,22 @@ Most of the data access operations you usually trigger on a repository result a
----
public interface PersonRepository extends PagingAndSortingRepository<Person, String> {
Person findByShippingAddresses(Address address); <3>
Stream<Person> findAllBy(); <4>
}
----
<1> The 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}`.
<2> Applies pagination 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.
<3> Shows that you can query based on properties which are not a primitive type.
<4> Uses a Java 8 `Stream` which reads and converts individual elements while iterating the stream.
====
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. The third examples shows that you can query based on properties which are not a primitive type.
NOTE: Note that for version 1.0 we currently don't support referring to parameters that are mapped as `DBRef` in the domain class.
@ -154,6 +160,10 @@ NOTE: Note that for version 1.0 we currently don't support referring to paramete
@@ -154,6 +160,10 @@ NOTE: Note that for version 1.0 we currently don't support referring to paramete
| Sample
| Logical result
| `After`
| `findByBirthdateAfter(Date date)`
| `{"birthdate" : {"$gt" : date}}`
| `GreaterThan`
| `findByAgeGreaterThan(int age)`
| `{"age" : {"$gt" : age}}`
@ -162,6 +172,10 @@ NOTE: Note that for version 1.0 we currently don't support referring to paramete
@@ -162,6 +172,10 @@ NOTE: Note that for version 1.0 we currently don't support referring to paramete
| `findByAgeGreaterThanEqual(int age)`
| `{"age" : {"$gte" : age}}`
| `Before`
| `findByBirthdateBefore(Date date)`
| `{"birthdate" : {"$lt" : date}}`
| `LessThan`
| `findByAgeLessThan(int age)`
| `{"age" : {"$lt" : age}}`
@ -190,10 +204,18 @@ NOTE: Note that for version 1.0 we currently don't support referring to paramete
@@ -190,10 +204,18 @@ NOTE: Note that for version 1.0 we currently don't support referring to paramete
@ -21,7 +21,7 @@ For most tasks you will find yourself using `MongoTemplate` or the Repository su
@@ -21,7 +21,7 @@ For most tasks you will find yourself using `MongoTemplate` or the Repository su
[[mongodb-getting-started]]
== Getting Started
Spring MongoDB support requires MongoDB 1.4 or higher and Java SE 5 or higher. The latest production release (2.4.9 as of this writing) is recommended. An easy way to bootstrap setting up a working environment is to create a Spring based project in http://spring.io/tools/sts[STS].
Spring MongoDB support requires MongoDB 2.6 or higher and Java SE 6 or higher. An easy way to bootstrap setting up a working environment is to create a Spring based project in http://spring.io/tools/sts[STS].
First you need to set up a running Mongodb server. Refer to the http://docs.mongodb.org/manual/core/introduction/[Mongodb Quick Start guide] for an explanation on how to startup a MongoDB instance. Once installed starting MongoDB is typically a matter of executing the following command: `MONGO_HOME/bin/mongod`
@ -38,7 +38,7 @@ Then add the following to pom.xml dependencies section.
@@ -38,7 +38,7 @@ Then add the following to pom.xml dependencies section.
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.4.1.RELEASE</version>
<version>1.7.0.RELEASE</version>
</dependency>
</dependencies>
@ -48,7 +48,7 @@ Also change the version of Spring in the pom.xml to be
@@ -48,7 +48,7 @@ Also change the version of Spring in the pom.xml to be
You will also need to add the location of the Spring Milestone repository for maven to your pom.xml which is at the same level of your <dependencies/> element
@ -162,7 +162,7 @@ Even in this simple example, there are few things to take notice of
@@ -162,7 +162,7 @@ Even in this simple example, there are few things to take notice of
[[mongo.examples-repo]]
== Examples Repository
There is an https://github.com/spring-projects/spring-data-document-examples[github repository with several examples] that you can download and play around with to get a feel for how the library works.
There is an https://github.com/spring-projects/spring-data-examples[github repository with several examples] that you can download and play around with to get a feel for how the library works.
[[mongodb-connectors]]
== Connecting to MongoDB with Spring
@ -1156,6 +1156,96 @@ As you can see we use the `NearQuery` builder API to set up a query to return al
@@ -1156,6 +1156,96 @@ As you can see we use the `NearQuery` builder API to set up a query to return al
The geo near operations return a `GeoResults` wrapper object that encapsulates `GeoResult` instances. The wrapping `GeoResults` allows to access the average distance of all results. A single `GeoResult` object simply carries the entity found plus its distance from the origin.
[[mongo.geo-json]]
=== GeoJSON Support
MongoDB supports http://geojeson.org/[GeoJSON] and simple (legacy) coordinate pairs for geospatial data. Those formats can both be used for storing as well as querying data.
NOTE: Please refer to the http://docs.mongodb.org/manual/core/2dsphere/#geospatial-indexes-store-geojson/[MongoDB manual on GeoJSON support] to learn about requirements and restrictions.
==== GeoJSON types in domain classes
Usage of http://geojeson.org/[GeoJSON] types in domain classes is straight forward. The `org.springframework.data.mongodb.core.geo` package contains types like `GeoJsonPoint`, `GeoJsonPolygon` and others. Those are extensions to the existing `org.springframework.data.geo` types.
====
[source,java]
----
public class Store {
String id;
/**
* location is stored in GeoJSON format.
* {
* "type" : "Point",
* "coordinates" : [ x, y ]
* }
*/
GeoJsonPoint location;
}
----
====
==== GeoJSON types in repository query methods
Using GeoJSON types as repository query parameters forces usage of the `$geometry` operator when creating the query.
====
[source,java]
----
public interface StoreRepository extends CrudRepository<Store, String> {
<1> Repository method definition using the commons type allows calling it with both GeoJSON and legacy format.
<2> Use GeoJSON type the make use of `$geometry` operator.
<3> Plase note that GeoJSON polygons need the define a closed ring.
<4> Use legacy format `$polygon` operator.
====
[[mongo.textsearch]]
=== Full Text Queries
@ -1340,17 +1430,21 @@ MongoDB allows to execute JavaScript functions on the server by either directly
@@ -1340,17 +1430,21 @@ MongoDB allows to execute JavaScript functions on the server by either directly