13.07.2015 Views

Spring Data MongoDB - Spring Web Services - Parent - SpringSource

Spring Data MongoDB - Spring Web Services - Parent - SpringSource

Spring Data MongoDB - Spring Web Services - Parent - SpringSource

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

please define productname in your docbook file!Point point = new Point(43.7, 48.8);Distance distance = new Distance(200, Metrics.KILOMETERS);… = repository.findByLocationNear(point, distance);// {'location' : {'$nearSphere' : [43.7, 48.8], '$maxDistance' : 0.03135711885774796}}Example 6.8 Using Distance with MetricsAs you can see using a Distance equipped with a Metric causes $nearSphere clause to be addedinstead of a plain $near. Beyond that the actual distance gets calculated according to the Metricsused.Geo-near queriespublic interface PersonRepository extends MongoRepository// {'geoNear' : 'location', 'near' : [x, y] }GeoResults findByLocationNear(Point location);// No metric: {'geoNear' : 'person', 'near' : [x, y], maxDistance : distance }// Metric: {'geoNear' : 'person', 'near' : [x, y], 'maxDistance' : distance,// 'distanceMultiplier' : metric.multiplier, 'spherical' : true }GeoResults findByLocationNear(Point location, Distance distance);}// {'geoNear' : 'location', 'near' : [x, y] }GeoResults findByLocationNear(Point location);<strong>MongoDB</strong> JSON based query methods and field restrictionBy adding the annotation org.springframework.data.mongodb.repository.Query repositoryfinder methods you can specify a <strong>MongoDB</strong> JSON query string to use instead of having the queryderived from the method name. For examplepublic interface PersonRepository extends MongoRepository@Query("{ 'firstname' : ?0 }")List findByThePersonsFirstname(String firstname);}The placeholder ?0 lets you substitute the value from the method arguments into the JSON query string.You can also use the filter property to restrict the set of properties that will be mapped into the Javaobject. For example,public interface PersonRepository extends MongoRepository@Query(value="{ 'firstname' : ?0 }", fields="{ 'firstname' : 1, 'lastname' : 1}")List findByThePersonsFirstname(String firstname);}This will return only the firstname, lastname and Id properties of the Person objects. The age property,a java.lang.Integer, will not be set and its value will therefore be null.1.4.0.BUILD-SNAPSHOT<strong>Spring</strong> <strong>Data</strong> <strong>MongoDB</strong> -Reference Documentation 74

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!