Browse Source

DATAMONGO-1974 - Polishing.

Fix typos, links, and code fences.

Original pull request: #559.
pull/585/head
Mark Paluch 8 years ago
parent
commit
f56bb16a12
  1. 2
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java
  2. 6
      src/main/asciidoc/index.adoc
  3. 6
      src/main/asciidoc/preface.adoc
  4. 8
      src/main/asciidoc/reference/mapping.adoc
  5. 2
      src/main/asciidoc/reference/mongo-3.adoc
  6. 22
      src/main/asciidoc/reference/mongo-repositories.adoc
  7. 59
      src/main/asciidoc/reference/mongodb.adoc

2
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java

@ -821,7 +821,7 @@ public interface MongoOperations { @@ -821,7 +821,7 @@ public interface MongoOperations {
* Insert a mixed Collection of objects into a database collection determining the collection name to use based on the
* class.
*
* @param collectionToSave the list of objects to save.
* @param objectsToSave the list of objects to save.
*/
void insertAll(Collection<? extends Object> objectsToSave);

6
src/main/asciidoc/index.adoc

@ -2,11 +2,11 @@ @@ -2,11 +2,11 @@
Mark Pollack; Thomas Risberg; Oliver Gierke; Costin Leau; Jon Brisbin; Thomas Darimont; Christoph Strobl; Mark Paluch; Jay Bryant
:revnumber: {version}
:revdate: {localdate}
:toc:
:toc-placement!:
:linkcss:
:doctype: book
:docinfo: shared
:toc: left
:toclevels: 4
:source-highlighter: prettify
:icons: font
:imagesdir: images
@ -17,6 +17,8 @@ ifdef::backend-epub3[:front-cover-image: image:epub-cover.png[Front Cover,1050,1 @@ -17,6 +17,8 @@ ifdef::backend-epub3[:front-cover-image: image:epub-cover.png[Front Cover,1050,1
NOTE: Copies of this document may be made for your own use and for distribution to others, provided that you do not charge any fee for such copies and further provided that each copy contains this Copyright Notice, whether distributed in print or electronically.
toc::[]
include::preface.adoc[]
include::new-features.adoc[leveloffset=+1]

6
src/main/asciidoc/preface.adoc

@ -3,7 +3,7 @@ @@ -3,7 +3,7 @@
The Spring Data MongoDB project applies core Spring concepts to the development of solutions that use the MongoDB document style data store. We provide a "`template`" as a high-level abstraction for storing and querying documents. You may notice similarities to the JDBC support provided by the Spring Framework.
This document is the reference guide for Spring Data - Document Support. It explains Document module concepts and semantics and syntax for various store namespaces.
This document is the reference guide for Spring Data - MongoDB Support. It explains MongoDB module concepts and semantics and syntax for various store namespaces.
This section provides some basic introduction to Spring and Document databases. The rest of the document refers only to Spring Data MongoDB features and assumes the user is familiar with MongoDB and Spring concepts.
@ -39,7 +39,7 @@ The starting point for learning about MongoDB is http://www.mongodb.org/[www.mon @@ -39,7 +39,7 @@ The starting point for learning about MongoDB is http://www.mongodb.org/[www.mon
[[requirements]]
== Requirements
The Spring Data MongoDB 1.x binaries require JDK level 6.0 and above and http://spring.io/docs[Spring Framework] {springVersion} and above.
The Spring Data MongoDB 2.x binaries require JDK level 8.0 and above and http://spring.io/docs[Spring Framework] {springVersion} and above.
In terms of document stores, you need at least version 2.6 of http://www.mongodb.org/[MongoDB].
@ -57,4 +57,4 @@ Professional Support :: Professional, from-the-source support, with guaranteed r @@ -57,4 +57,4 @@ Professional Support :: Professional, from-the-source support, with guaranteed r
[[get-started:up-to-date]]
== Following Development
For information on the Spring Data Mongo source code repository, nightly builds, and snapshot artifacts, see the Spring Data Mongo http://projects.spring.io/spring-data-mongodb/[homepage]. You can help make Spring Data best serve the needs of the Spring community by interacting with developers through the Community on http://stackoverflow.com/questions/tagged/spring-data[Stack Overflow]. To follow developer activity, look for the mailing list information on the Spring Data Mongo https://projects.spring.io/spring-data-mongodb/[homepage]. If you encounter a bug or want to suggest an improvement, please create a ticket on the Spring Data issue https://jira.spring.io/browse/DATAMONGO[tracker]. To stay up to date with the latest news and announcements in the Spring eco system, subscribe to the Spring Community http://spring.io[Portal]. You can also follow the Spring http://spring.io/blog[blog ]or the project team on Twitter (http://twitter.com/SpringData[SpringData]).
For information on the Spring Data Mongo source code repository, nightly builds, and snapshot artifacts, see the Spring Data Mongo http://projects.spring.io/spring-data-mongodb/[homepage]. You can help make Spring Data best serve the needs of the Spring community by interacting with developers through the Community on http://stackoverflow.com/questions/tagged/spring-data[Stack Overflow]. To follow developer activity, look for the mailing list information on the Spring Data Mongo https://projects.spring.io/spring-data-mongodb/[homepage]. If you encounter a bug or want to suggest an improvement, please create a ticket on the Spring Data issue https://jira.spring.io/browse/DATAMONGO[tracker]. To stay up to date with the latest news and announcements in the Spring eco system, subscribe to the Spring Community http://spring.io[Portal]. You can also follow the Spring http://spring.io/blog[blog] or the project team on Twitter (http://twitter.com/SpringData[SpringData]).

8
src/main/asciidoc/reference/mapping.adoc

@ -252,7 +252,7 @@ calling `get()` before the actual conversion @@ -252,7 +252,7 @@ calling `get()` before the actual conversion
[[mapping-configuration]]
== Mapping Configuration
Unless explicitly configured, an instance of `MappingMongoConverter` is created by default when you create a `MongoTemplate`. You can create your own instance of the `MappingMongoConverter`. Doing so lets you dictate where to in the classpath your domain classes can be found, so that Spring Data MongoDB can extract metadata and construct indexes. Also, by creating your own instance, you can register Spring converters to map specific classes to and from the database.
Unless explicitly configured, an instance of `MappingMongoConverter` is created by default when you create a `MongoTemplate`. You can create your own instance of the `MappingMongoConverter`. Doing so lets you dictate where in the classpath your domain classes can be found, so that Spring Data MongoDB can extract metadata and construct indexes. Also, by creating your own instance, you can register Spring converters to map specific classes to and from the database.
You can configure the `MappingMongoConverter` as well as `com.mongodb.Mongo` and MongoTemplate by using either Java-based or XML-based metadata. The following example uses Spring's Java-based configuration:
@ -300,12 +300,10 @@ public class GeoSpatialAppConfig extends AbstractMongoConfiguration { @@ -300,12 +300,10 @@ public class GeoSpatialAppConfig extends AbstractMongoConfiguration {
`AbstractMongoConfiguration` requires you to implement methods that define a `com.mongodb.Mongo` as well as provide a database name. `AbstractMongoConfiguration` also has a method named `getMappingBasePackage(…)` that you can override to tell the converter where to scan for classes annotated with the `@Document` annotation.
You can add additional converters to the converter by overriding the `afterMappingMongoConverterCreation` method. Also shown in the preceding example is a `LoggingEventListener`, which logs `MongoMappingEvent` instances that are posted onto Spring's `ApplicationContextEvent` infrastructure.
You can add additional converters to the converter by overriding the `customConversions` method. Also shown in the preceding example is a `LoggingEventListener`, which logs `MongoMappingEvent` instances that are posted onto Spring's `ApplicationContextEvent` infrastructure.
NOTE: `AbstractMongoConfiguration` creates a `MongoTemplate` instance and registers it with the container under the name `mongoTemplate`.
You can also override the `UserCredentials getUserCredentials()` method to provide the username and password information to connect to the database.
Spring's MongoDB namespace lets you enable mapping functionality in XML, as the following example shows:
.XML schema to configure MongoDB mapping support
@ -591,7 +589,7 @@ public class Person { @@ -591,7 +589,7 @@ public class Person {
----
====
You need not use `@OneToMany` or similar mechanisms because the List of objects tells the mapping framework sees that you want a one-to-many relationship. When the object is stored in MongoDB, there is a list of DBRefs rather than the `Account` objects themselves.
You need not use `@OneToMany` or similar mechanisms because the List of objects tells the mapping framework that you want a one-to-many relationship. When the object is stored in MongoDB, there is a list of DBRefs rather than the `Account` objects themselves.
IMPORTANT: The mapping framework does not handle cascading saves. If you change an `Account` object that is referenced by a `Person` object, you must save the `Account` object separately. Calling `save` on the `Person` object does not automatically save the `Account` objects in the `accounts` property.

2
src/main/asciidoc/reference/mongo-3.adoc

@ -1,7 +1,7 @@ @@ -1,7 +1,7 @@
[[mongo.mongo-3]]
= MongoDB 3.0 Support
Spring Data MongoDB allows usage of both MongoDB Java driver generations 2 and 3 when connecting to a MongoDB 2.6/3.0 server running MMap.v1 or a MongoDB server 3.0 using MMap.v1 or the WiredTiger storage engine.
Spring Data MongoDB requires MongoDB Java driver generations 3 when connecting to a MongoDB 2.6/3.0 server running MMap.v1 or a MongoDB server 3.0 using MMap.v1 or the WiredTiger storage engine.
NOTE: See the driver- and database-specific documentation for major differences between those engines.

22
src/main/asciidoc/reference/mongo-repositories.adoc

@ -9,7 +9,7 @@ This chapter points out the specialties for repository support for MongoDB. This @@ -9,7 +9,7 @@ This chapter points out the specialties for repository support for MongoDB. This
[[mongo-repo-usage]]
== Usage
To access domain entities stored in a MongoDBm, you can use our sophisticated repository support that eases implementation quite significantly. To do so, create an interface for your repository, as the following example shows:
To access domain entities stored in a MongoDB, you can use our sophisticated repository support that eases implementation quite significantly. To do so, create an interface for your repository, as the following example shows:
.Sample Person entity
====
@ -36,7 +36,7 @@ Note that the domain type shown in the preceding example has a property named `i @@ -36,7 +36,7 @@ Note that the domain type shown in the preceding example has a property named `i
----
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
// additional custom finder methods go here
// additional custom query methods go here
}
----
====
@ -152,7 +152,7 @@ public interface PersonRepository extends PagingAndSortingRepository<Person, Str @@ -152,7 +152,7 @@ public interface PersonRepository extends PagingAndSortingRepository<Person, Str
NOTE: For version 1.0, we currently do not support referring to parameters that are mapped as `DBRef` in the domain class.
NOTE: We do not support referring to parameters that are mapped as `DBRef` in the domain class.
The following table shows the keywords that are supported for query methods:
@ -315,7 +315,7 @@ public interface PersonRepository extends MongoRepository<Person, String> @@ -315,7 +315,7 @@ public interface PersonRepository extends MongoRepository<Person, String>
----
====
Adding a `Distance` parameter to the query method allows restricting results to those within the given distance. If the `Distance` was set up containing a `Metric`, we transparently use `$nearSphere` instead of $code, as the following example shows:
Adding a `Distance` parameter to the query method allows restricting results to those within the given distance. If the `Distance` was set up containing a `Metric`, we transparently use `$nearSphere` instead of `$code`, as the following example shows:
.Using `Distance` with `Metrics`
====
@ -363,7 +363,7 @@ public interface PersonRepository extends MongoRepository<Person, String> @@ -363,7 +363,7 @@ public interface PersonRepository extends MongoRepository<Person, String>
[[mongodb.repositories.queries.json-based]]
=== MongoDB JSON-based Query Methods and Field Restriction
By adding the `org.springframework.data.mongodb.repository.Query` annotation to your repository finder methods, you can specify a MongoDB JSON query string to use instead of having the query be derived from the method name, as the following example shows:
By adding the `org.springframework.data.mongodb.repository.Query` annotation to your repository query methods, you can specify a MongoDB JSON query string to use instead of having the query be derived from the method name, as the following example shows:
[source,java]
----
@ -391,7 +391,7 @@ public interface PersonRepository extends MongoRepository<Person, String> @@ -391,7 +391,7 @@ public interface PersonRepository extends MongoRepository<Person, String>
}
----
The query in the preceding example returns only the `firstname`, `lastname` and `Id` properties of the `Person` objects. The `age` property, a java.lang.Integer, is not set and its value is therefore null.
The query in the preceding example returns only the `firstname`, `lastname` and `Id` properties of the `Person` objects. The `age` property, a `java.lang.Integer`, is not set and its value is therefore null.
[[mongodb.repositories.queries.json-spel]]
=== JSON-based Queries with SpEL Expressions
@ -452,7 +452,7 @@ to pick up Query SPI extensions. @@ -452,7 +452,7 @@ to pick up Query SPI extensions.
[[mongodb.repositories.queries.type-safe]]
=== Type-safe Query Methods
MongoDB repository support integrates with the http://www.querydsl.com/[QueryDSL] project, which provides a way to perform type-safe queries. To quote from the project description, "Instead of writing queries as inline strings or externalizing them into XML files they are constructed via a fluent API." It provides the following features:
MongoDB repository support integrates with the http://www.querydsl.com/[Querydsl] project, which provides a way to perform type-safe queries. To quote from the project description, "Instead of writing queries as inline strings or externalizing them into XML files they are constructed via a fluent API." It provides the following features:
* Code completion in the IDE (all properties, methods, and operations can be expanded in your favorite Java IDE).
* Almost no syntactically invalid queries allowed (type-safe on all levels).
@ -475,11 +475,11 @@ Page<Person> page = repository.findAll(person.lastname.contains("a"), @@ -475,11 +475,11 @@ Page<Person> page = repository.findAll(person.lastname.contains("a"),
`QPerson` is a class that is generated by the Java annotation post-processing tool. It is a `Predicate` that lets you write type-safe queries. Notice that there are no strings in the query other than the `C0123` value.
You can use the generated `Predicate` class by using the `QueryDslPredicateExecutor` interface, which the following listing shows:
You can use the generated `Predicate` class by using the `QuerydslPredicateExecutor` interface, which the following listing shows:
[source,java]
----
public interface QueryDslPredicateExecutor<T> {
public interface QuerydslPredicateExecutor<T> {
T findOne(Predicate predicate);
@ -497,9 +497,9 @@ To use this in your repository implementation, add it to the list of repository @@ -497,9 +497,9 @@ To use this in your repository implementation, add it to the list of repository
[source,java]
----
public interface PersonRepository extends MongoRepository<Person, String>, QueryDslPredicateExecutor<Person> {
public interface PersonRepository extends MongoRepository<Person, String>, QuerydslPredicateExecutor<Person> {
// additional finder methods go here
// additional query methods go here
}
----

59
src/main/asciidoc/reference/mongodb.adoc

@ -3,7 +3,7 @@ @@ -3,7 +3,7 @@
The MongoDB support contains a wide range of features:
* Spring configuration support with Java-based @Configuration classes or an XML namespace for a Mongo driver instance and replica sets.
* Spring configuration support with Java-based `@Configuration` classes or an XML namespace for a Mongo driver instance and replica sets.
* `MongoTemplate` helper class that increases productivity when performing common Mongo operations. Includes integrated object mapping between documents and POJOs.
* Exception translation into Spring's portable Data Access Exception hierarchy.
* Feature-rich Object Mapping integrated with Spring's Conversion Service.
@ -23,7 +23,7 @@ For most tasks, you should use `MongoTemplate` or the Repository support, which @@ -23,7 +23,7 @@ For most tasks, you should use `MongoTemplate` or the Repository support, which
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 running the following command: `MONGO_HOME/bin/mongod`
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 running the following command: `${MONGO_HOME}/bin/mongod`
To create a Spring project in STS:
@ -153,7 +153,7 @@ When you run the main program, the preceding examples produce the following outp @@ -153,7 +153,7 @@ When you run the main program, the preceding examples produce the following outp
Even in this simple example, there are few things to notice:
* You can instantiate the central helper class of Spring Mongo, <<mongo-template,`MongoTemplate`>>, by using the standard `com.mongodb.Mongo` object and the name of the database to use.
* The mapper works against standard POJO objects without the need for any additional metadata (though you can optionally provide that information. See <<mongo.mapping,here>>.).
* The mapper works against standard POJO objects without the need for any additional metadata (though you can optionally provide that information. See <<mapping-chapter,here>>.).
* Conventions are used for handling the `id` field, converting it to be an `ObjectId` when stored in the database.
* Mapping conventions can use field access. Notice that the `Person` class has only getters.
* If the constructor argument names match the field names of the stored document, they are used to instantiate the object
@ -161,7 +161,7 @@ Even in this simple example, there are few things to notice: @@ -161,7 +161,7 @@ Even in this simple example, there are few things to notice:
[[mongo.examples-repo]]
== Examples Repository
There is a 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.
There is a 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
@ -237,13 +237,13 @@ To use the Mongo namespace elements, you need to reference the Mongo schema, as @@ -237,13 +237,13 @@ To use the Mongo namespace elements, you need to reference the Mongo schema, as
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation=
"http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-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/spring-context.xsd
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Default bean name is 'mongo' -->
*<mongo:mongo host="localhost" port="27017"/>*
<mongo:mongo host="localhost" port="27017"/>
</beans>
----
@ -314,7 +314,7 @@ public class MongoApp { @@ -314,7 +314,7 @@ public class MongoApp {
public static void main(String[] args) throws Exception {
MongoOperations mongoOps = new MongoTemplate(*new SimpleMongoDbFactory(new Mongo(), "database")*);
MongoOperations mongoOps = new MongoTemplate(new SimpleMongoDbFactory(new Mongo(), "database"));
mongoOps.insert(new Person("Joe", 34));
@ -362,6 +362,7 @@ public class MongoConfiguration { @@ -362,6 +362,7 @@ public class MongoConfiguration {
----
[[mongo.mongo-db-factory-xml]]
=== Registering a `MongoDbFactory` Instance by Using XML-based Metadata
@ -441,22 +442,22 @@ The `MongoTemplate` class implements the interface `MongoOperations`. In as much @@ -441,22 +442,22 @@ The `MongoTemplate` class implements the interface `MongoOperations`. In as much
NOTE: The preferred way to reference the operations on `MongoTemplate` instance is through its interface, `MongoOperations`.
The default converter implementation used by `MongoTemplate` is `MappingMongoConverter`. While the `MappingMongoConverter` can use additional metadata to specify the mapping of objects to documents, it can also convert objects that contain no additional metadata by using some conventions for the mapping of IDs and collection names. These conventions, as well as the use of mapping annotations, are explained in the "`<<mongo.mapping>`"> chapter.
The default converter implementation used by `MongoTemplate` is `MappingMongoConverter`. While the `MappingMongoConverter` can use additional metadata to specify the mapping of objects to documents, it can also convert objects that contain no additional metadata by using some conventions for the mapping of IDs and collection names. These conventions, as well as the use of mapping annotations, are explained in the "`<<mapping-chapter>>`" chapter.
NOTE: In the M2 release `SimpleMappingConverter`, was the default and this class is now deprecated as its functionality has been subsumed by the `MappingMongoConverter`.
Another central feature of `MongoTemplate` is translation of exceptions thrown by the MongoDB Java driver into Spring's portable Data Access Exception hierarchy. See "`<<mongo.exception>>`" for more information.
`MongoTemplate` offers many convenience methods to help you easily perform common tasks. However, if you need to directly access the MongoDB driver API, you can use one of several `Execute` callback methods. The execute callbacks gives you a reference to either a `com.mongodb.Collection` or a `com.mongodb.DB` object. See the mongo.executioncallback["`Execution Callbacks`"] section for more information.
`MongoTemplate` offers many convenience methods to help you easily perform common tasks. However, if you need to directly access the MongoDB driver API, you can use one of several `Execute` callback methods. The execute callbacks gives you a reference to either a `com.mongodb.DBCollection` or a `com.mongodb.DB` object. See the <<mongo.executioncallback,"`Execution Callbacks`">> section for more information.
The next section contains an example of how to work with the `MongoTemplate` in the context of the Spring container.
[[mongo-template.instantiating]]
=== Instantiating MongoTemplate
=== Instantiating `MongoTemplate`
You can use Java to create and register an instance of `MongoTemplate`, as the following example shows:
.Registering a com.mongodb.Mongo object and enabling Spring's exception translation support
.Registering a `com.mongodb.Mongo` object and enabling Spring's exception translation support
====
[source,java]
----
@ -539,7 +540,7 @@ private class MyAppWriteConcernResolver implements WriteConcernResolver { @@ -539,7 +540,7 @@ private class MyAppWriteConcernResolver implements WriteConcernResolver {
[[mongo-template.save-update-remove]]
== Saving, Updating, and Removing Documents
`MongoTemplate` provides lets you save, update, and delete your domain objects and map those objects to documents stored in MongoDB.
`MongoTemplate` lets you save, update, and delete your domain objects and map those objects to documents stored in MongoDB.
Consider the following class:
@ -1025,7 +1026,7 @@ template.save(tmp); // throws OptimisticLockingFailureException @@ -1025,7 +1026,7 @@ template.save(tmp); // throws OptimisticLockingFailureException
<4> Try to update the previously loaded document that still has `version = 0`. The operation fails with an `OptimisticLockingFailureException`, as the current `version` is `1`.
====
IMPORTANT: Using MongoDB driver version 3 requires to set the `WriteConcern` to `ACKNOWLEDGED`. Otherwise `OptimisticLockingFailureException` can be silently swallowed.
IMPORTANT: Optimistic Locking requires to set the `WriteConcern` to `ACKNOWLEDGED`. Otherwise `OptimisticLockingFailureException` can be silently swallowed.
[[mongo.query]]
== Querying Documents
@ -1118,9 +1119,9 @@ The `Query` class has some additional methods that provide options for the query @@ -1118,9 +1119,9 @@ The `Query` class has some additional methods that provide options for the query
[[mongo-template.querying]]
=== Methods for Querying for Documents
The query methods need to specify the target type T that is returned, and they are overloaded with an explicit collection name for queries that should operate on a collection other than the one indicated by the return type. The following query methods let you find one or more documents:
The query methods need to specify the target type `T` that is returned, and they are overloaded with an explicit collection name for queries that should operate on a collection other than the one indicated by the return type. The following query methods let you find one or more documents:
* *findAll*: Query for a list of objects of type T from the collection.
* *findAll*: Query for a list of objects of type `T` from the collection.
* *findOne*: Map the results of an ad-hoc query on the collection to a single instance of an object of the specified type.
* *findById*: Return an object of the given ID and target class.
* *find*: Map the results of an ad-hoc query on the collection to a `List` of the specified type.
@ -1240,7 +1241,7 @@ NearQuery query = NearQuery.near(location).maxDistance(new Distance(10, Metrics. @@ -1240,7 +1241,7 @@ NearQuery query = NearQuery.near(location).maxDistance(new Distance(10, Metrics.
GeoResults<Restaurant> = operations.geoNear(query, Restaurant.class);
----
We use the `NearQuery` builder API to set up a query to return all `Restaurant` instances surrounding the given `Point` out to 10 miles. 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 built-in metrics (miles and kilometers) automatically triggers the spherical flag to be set on the query. If you want to avoid that, pass plain `double` values into `maxDistance(…)`. For more information, see the https://docs.spring.io/spring-data/mongodb/docs/current/api/index.html[JavaDoc] of `NearQuery` and `Distance`.
We use the `NearQuery` builder API to set up a query to return all `Restaurant` instances surrounding the given `Point` out to 10 miles. 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 built-in metrics (miles and kilometers) automatically triggers the spherical flag to be set on the query. If you want to avoid that, pass plain `double` values into `maxDistance(…)`. For more information, see the https://docs.spring.io/spring-data/mongodb/docs/{version}/api/index.html[JavaDoc] of `NearQuery` and `Distance`.
The geo-near operations return a `GeoResults` wrapper object that encapsulates `GeoResult` instances. Wrapping `GeoResults` allows accessing the average distance of all results. A single `GeoResult` object carries the entity found plus its distance from the origin.
@ -1487,7 +1488,7 @@ public class ValueObject { @@ -1487,7 +1488,7 @@ public class ValueObject {
}
----
By default, the output type of INLINE is used so that you need not specify an output collection. To specify additional Map-Reduce options, use an overloaded method that takes an additional `MapReduceOptions` argument. The class `MapReduceOptions` has a fluent API, so adding additional options can be done in a compact syntax. The following example sets the output collection to `jmr1_out` (note that setting only the output collection assumes a default output type of `REPLACE`):
By default, the output type of `INLINE` is used so that you need not specify an output collection. To specify additional Map-Reduce options, use an overloaded method that takes an additional `MapReduceOptions` argument. The class `MapReduceOptions` has a fluent API, so adding additional options can be done in a compact syntax. The following example sets the output collection to `jmr1_out` (note that setting only the output collection assumes a default output type of `REPLACE`):
[source,java]
----
@ -1537,7 +1538,7 @@ scriptOps.call("echo", "execute script via name"); <3> @@ -1537,7 +1538,7 @@ scriptOps.call("echo", "execute script via name"); <3>
[[mongo.group]]
== Group Operations
{JB}
As an alternative to using Map-Reduce to perform data aggregation, you can use the http://www.mongodb.org/display/DOCS/Aggregation#Aggregation-Group[`group` operation] which feels similar to using SQL's group by query style, so it may feel more approachable vs. using Map-Reduce. Using the group operations does have some limitations, for example it is not supported in a shared environment and it returns the full result set in a single BSON object, so the result should be small, less than 10,000 keys.
Spring provides integration with MongoDB's group operation by providing methods on MongoOperations to simplify the creation and execution of group operations. It can convert the results of the group operation to a POJO and also integrates with Spring's http://docs.spring.io/spring/docs/{springVersion}/spring-framework-reference/html/resources.html[Resource abstraction] abstraction. This will let you place your JavaScript files on the file system, classpath, http server or any other Spring Resource implementation and then reference the JavaScript resources via an easy URI style syntax, e.g. 'classpath:reduce.js;. Externalizing JavaScript code in files if often preferable to embedding them as Java strings in your code. Note that you can still pass JavaScript code as Java strings if you prefer.
@ -1765,11 +1766,11 @@ project("a","b").and("thing1").as("thing2") @@ -1765,11 +1766,11 @@ project("a","b").and("thing1").as("thing2")
// generates {$project: {name: 1, netPrice: 1}}, {$sort: {name: 1}}
project("name", "netPrice"), sort(ASC, "name")
// generates {$project: {thing2: $thing1}}, {$sort: {thing2: 1}}
project().and("thing1").as("thing2"), sort(ASC, "thing2")
// generates {$project: {name: $firstname}}, {$sort: {name: 1}}
project().and("firstname").as("name"), sort(ASC, "name")
// does not work
project().and("thing1").as("thing2"), sort(ASC, "thing1")
project().and("firstname").as("name"), sort(ASC, "firstname")
----
====
@ -1782,7 +1783,7 @@ As of Version 3.4, MongoDB supports faceted classification by using the Aggregat @@ -1782,7 +1783,7 @@ As of Version 3.4, MongoDB supports faceted classification by using the Aggregat
==== Buckets
Bucket operations categorize incoming documents into groups, called buckets, based on a specified expression and bucket boundaries. Bucket operations require a grouping field or a grouping expression. You can define them by using the `bucket()` and `bucketAuto()` methods of the `Aggregate` class. `BucketOperation` and `BucketAutoOperation` can expose accumulations based on aggregation expressions for input documents. You can extend the bucket operation with additional parameters through a fluent API by using the `with…()` methods and the `andOutput(String)` method. You can alias alias the operation by using the `as(String)` method. Each bucket is represented as a document in the output.
Bucket operations categorize incoming documents into groups, called buckets, based on a specified expression and bucket boundaries. Bucket operations require a grouping field or a grouping expression. You can define them by using the `bucket()` and `bucketAuto()` methods of the `Aggregate` class. `BucketOperation` and `BucketAutoOperation` can expose accumulations based on aggregation expressions for input documents. You can extend the bucket operation with additional parameters through a fluent API by using the `with…()` methods and the `andOutput(String)` method. You can alias the operation by using the `as(String)` method. Each bucket is represented as a document in the output.
`BucketOperation` takes a defined set of boundaries to group incoming documents into these categories. Boundaries are required to be sorted. The following listing shows some examples of bucket operations:
@ -1863,7 +1864,7 @@ We support the use of SpEL expressions in projection expressions through the `an @@ -1863,7 +1864,7 @@ We support the use of SpEL expressions in projection expressions through the `an
===== Complex Calculations with SpEL expressions
Consier the following SpEL expression:
Consider the following SpEL expression:
[source,java]
----
@ -2402,8 +2403,8 @@ mongoTemplate.dropCollection("MyNewCollection"); @@ -2402,8 +2403,8 @@ mongoTemplate.dropCollection("MyNewCollection");
* *getCollectionNames*: Returns a set of collection names.
* *collectionExists*: Checks to see if a collection with a given name exists.
* *createCollection*: Creates an uncapped collection
* *dropCollection*: Drops the collection
* *createCollection*: Creates an uncapped collection.
* *dropCollection*: Drops the collection.
* *getCollection*: Gets a collection by name, creating it if it does not exist.
[[mongo-template.commands]]
@ -2420,7 +2421,7 @@ You can get at the MongoDB driver's `DB.command( )` method by using the `execute @@ -2420,7 +2421,7 @@ You can get at the MongoDB driver's `DB.command( )` method by using the `execute
[[mongodb.mapping-usage.events]]
== Lifecycle Events
The MongoDB mapping framework includes several `org.springframework.context.ApplicationEvent` events that your application can respond to by registering special beans in the `ApplicationContext`. Being based off of Spring's `ApplicationContext` event infrastructure enables other products, such as Spring Integration, to easily receive these events, as they are a well known eventing mechanism in Spring-based applications.
The MongoDB mapping framework includes several `org.springframework.context.ApplicationEvent` events that your application can respond to by registering special beans in the `ApplicationContext`. Being based on Spring's `ApplicationContext` event infrastructure enables other products, such as Spring Integration, to easily receive these events, as they are a well known eventing mechanism in Spring-based applications.
To intercept an object before it goes through the conversion process (which turns your domain object into a `com.mongodb.DBObject`), you can register a subclass of `AbstractMongoEventListener` that overrides the `onBeforeConvert` method. When the event is dispatched, your listener is called and passed the domain object before it goes into the converter. The following example shows how to do so:
@ -2619,4 +2620,4 @@ class GridFsClient { @@ -2619,4 +2620,4 @@ class GridFsClient {
----
====
`GridFsOperations` extends `ResourcePatternResolver` and lets the `GridFsTemplate` (for exmaple) to be plugged into an `ApplicationContext` to read Spring Config files from MongoDB database.
`GridFsOperations` extends `ResourcePatternResolver` and lets the `GridFsTemplate` (for example) to be plugged into an `ApplicationContext` to read Spring Config files from MongoDB database.

Loading…
Cancel
Save