diff --git a/src/docbkx/reference/mongodb.xml b/src/docbkx/reference/mongodb.xml index f48e013e4..d0d853da7 100644 --- a/src/docbkx/reference/mongodb.xml +++ b/src/docbkx/reference/mongodb.xml @@ -242,7 +242,7 @@ public class MongoApp {
- + Required Jars @@ -1163,7 +1163,7 @@ import static org.springframework.data.document.mongodb.query.Criteria.query; "person" collection. You can customize this by providing a different collection name using the @Document annotation. You can also override the collection name by providing your own collection name as the last - parameter for the selected MongoTemplate method calls. + parameter for the selected MongoTemplate method calls.
@@ -1751,36 +1751,37 @@ public class Venue { } } - To find locations within a circle, the following query can be - used. + To find locations within a Circle, the + following query can be 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); - To find venues within a circle using Spherical coordinates the - following query can be used + 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); - To find venues within a Box the following query can be used + To find venues within a Box the following + query can be used //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 + 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); - To find venues near a Point using Spherical coordines the - following query can be used + 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 = @@ -1789,10 +1790,48 @@ List<Venue> venues = Venue.class); - - Support for the GeoNear command will be provided in the RC - release. - +
+ Geo near queries + + MongoDB supports querying the database for geo locations and + calculation the distance from a given origin at the very same time. + With geo-near queries it's possible to express queries like: "find all + restaurants in the surrounding 10 miles". To do so + MongoOperations provides + geoNear(…) methods taking a + NearQuery as argument as well as the already + familiar entity type and collection + + Point location = new Point(-73.99171, 40.738868); +NearQuery query = NearQuery.near(location).maxDistance(new Distance(10, Metrics.MILES)); + +GeoResults<Restaurant> = operations.geoNear(query, Restaurant.class); + + As you can see we use the NearQuery + builder API to set up a query to return all + Restaurant instances surrounding the given + Point by 10 miles maximum. The + Metrics enum used here actually implements an + interface so that other metrics could be plugged into a distance as + well. A Metric is backed by a + multiplier to transform the distance value of the given metric into + native distances. The sample shown here would consider the 10 to be + miles. Using one of the pre-built in metrics (miles and kilometers) + will automatically trigger the spherical flag to be set on the query. + If you want to avoid that, simply hand in plain + double values into + maxDistance(…). For more information see the + JavaDoc of NearQuery and + Distance. + + 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. +
@@ -1807,10 +1846,11 @@ List<Venue> venues = The MappingMongoConverter checks to see if there are any Spring converters that can handle a specific class before attempting to map the object itself. To 'hijack' the normal mapping - strategies of the MappingMongoConverter, perhaps for increased performance - or other custom mapping needs, you first need to create an implementation - of the Spring Converter interface and then - register it with the MappingConverter. + strategies of the MappingMongoConverter, perhaps + for increased performance or other custom mapping needs, you first need to + create an implementation of the Spring + Converter interface and then register it + with the MappingConverter. For more information on the Spring type conversion service see the @@ -2013,20 +2053,18 @@ mongoTemplate.dropCollection("MyNewCollection"); To intercept an object before it goes through the conversion process (which turns your domain object into a com.mongodb.DBObject), you'd register a subclass of - org.springframework.data.document.mongodb.mapping.event.AbstractMappingEventListener - that overrides the onBeforeConvert method. When the event is - dispatched, your listener will be called and passed the domain object - before it goes into the converter. + AbstractMongoEventListener that overrides the + onBeforeConvert method. When the event is dispatched, your + listener will be called and passed the domain object before it goes into + the converter. - -public class BeforeConvertListener<BeforeConvertEvent, Person> extends AbstractMappingEventListener { + public class BeforeConvertListener extends AbstractMongoEventListener<Person> { @Override public void onBeforeConvert(Person p) { ... does some auditing manipulation, set timestamps, whatever ... } -} - +} To intercept an object before it goes into the database, you'd @@ -2037,14 +2075,12 @@ public class BeforeConvertListener<BeforeConvertEvent, Person> extends Abs the converted com.mongodb.DBObject. - -public class BeforeSaveListener<BeforeSaveEvent, Person> extends AbstractMappingEventListener { + public class BeforeSaveListener extends AbstractMongoEventListener<Person> { @Override public void onBeforeSave(Person p, DBObject dbo) { - ... change values, delete them, whatever ... + … change values, delete them, whatever … } -} - +} Simply declaring these beans in your Spring ApplicationContext will @@ -2188,4 +2224,4 @@ public class BeforeSaveListener<BeforeSaveEvent, Person> extends AbstractM } }); - + \ No newline at end of file