diff --git a/.gitignore b/.gitignore
index be372b620..988224e62 100644
--- a/.gitignore
+++ b/.gitignore
@@ -15,4 +15,9 @@ src/ant/.ant-targets-upload-dist.xml
atlassian-ide-plugin.xml
/.gradle/
/.idea/
-*.graphml
\ No newline at end of file
+*.graphml
+build/
+node_modules
+node
+package.json
+package-lock.json
diff --git a/spring-data-mongodb-distribution/pom.xml b/spring-data-mongodb-distribution/pom.xml
index 060a6d0dd..bb9777a48 100644
--- a/spring-data-mongodb-distribution/pom.xml
+++ b/spring-data-mongodb-distribution/pom.xml
@@ -21,60 +21,35 @@
${basedir}/..
- SDMONGO
-
-
- ${maven.multiModuleProjectDirectory}/spring-data-mongodb/
- .*
- ${maven.multiModuleProjectDirectory}/target/
+ ${project.basedir}/../src/main/antora/antora-playbook.yml
+
+
+ ${project.basedir}/../src/main/antora/resources/antora-resources
+ true
+
+
+
+ org.apache.maven.plugins
+ maven-resources-plugin
+
+
+
+ resources
+
+
+
+ org.apache.maven.pluginsmaven-assembly-plugin
- org.codehaus.mojo
- exec-maven-plugin
-
-
- generate-docs
- generate-resources
-
- java
-
-
- io.micrometer.docs.DocsGeneratorCommand
- true
-
- ${micrometer-docs-generator.inputPath}
- ${micrometer-docs-generator.inclusionPattern}
- ${micrometer-docs-generator.outputPath}
-
-
-
-
-
-
- io.micrometer
- micrometer-docs-generator
- 1.0.1
- jar
-
-
-
-
- org.asciidoctor
- asciidoctor-maven-plugin
-
-
- ${mongo.reactivestreams}
-
- ${reactor}
-
-
+ io.spring.maven.antora
+ antora-maven-plugin
diff --git a/src/main/antora/antora-playbook.yml b/src/main/antora/antora-playbook.yml
new file mode 100644
index 000000000..82f813420
--- /dev/null
+++ b/src/main/antora/antora-playbook.yml
@@ -0,0 +1,42 @@
+# PACKAGES antora@3.2.0-alpha.2 @antora/atlas-extension:1.0.0-alpha.1 @antora/collector-extension@1.0.0-alpha.3 @springio/antora-extensions@1.1.0-alpha.2 @asciidoctor/tabs@1.0.0-alpha.12 @opendevise/antora-release-line-extension@1.0.0-alpha.2
+#
+# The purpose of this Antora playbook is to build the docs in the current branch.
+antora:
+ extensions:
+ - '@antora/collector-extension'
+ - require: '@springio/antora-extensions/root-component-extension'
+ root_component_name: 'data-mongodb'
+site:
+ title: Spring Data MongoDB
+ url: https://docs.spring.io/spring-data-mongodb/reference/
+content:
+ sources:
+ - url: ./../../..
+ branches: HEAD
+ start_path: src/main/antora
+ worktrees: true
+ - url: https://github.com/spring-projects/spring-data-commons
+ # Refname matching:
+ # https://docs.antora.org/antora/latest/playbook/content-refname-matching/
+ branches: [ main, 3.2.x ]
+ start_path: src/main/antora
+asciidoc:
+ attributes:
+ page-pagination: ''
+ hide-uri-scheme: '@'
+ tabs-sync-option: '@'
+ chomp: 'all'
+ extensions:
+ - '@asciidoctor/tabs'
+ - '@springio/asciidoctor-extensions'
+ sourcemap: true
+urls:
+ latest_version_segment: ''
+runtime:
+ log:
+ failure_level: warn
+ format: pretty
+ui:
+ bundle:
+ url: https://github.com/spring-io/antora-ui-spring/releases/download/v0.3.5/ui-bundle.zip
+ snapshot: true
diff --git a/src/main/antora/antora.yml b/src/main/antora/antora.yml
new file mode 100644
index 000000000..287d53bd9
--- /dev/null
+++ b/src/main/antora/antora.yml
@@ -0,0 +1,12 @@
+name: data-mongodb
+version: true
+title: Spring Data MongoDB
+nav:
+ - modules/ROOT/nav.adoc
+ext:
+ collector:
+ - run:
+ command: ./mvnw validate process-resources -pl :spring-data-mongodb-distribution -am -Pantora-process-resources
+ local: true
+ scan:
+ dir: spring-data-mongodb-distribution/target/classes/
diff --git a/src/main/antora/modules/ROOT/assets/images/epub-cover.png b/src/main/antora/modules/ROOT/assets/images/epub-cover.png
new file mode 100644
index 000000000..7be1d08b3
Binary files /dev/null and b/src/main/antora/modules/ROOT/assets/images/epub-cover.png differ
diff --git a/src/main/antora/modules/ROOT/assets/images/epub-cover.svg b/src/main/antora/modules/ROOT/assets/images/epub-cover.svg
new file mode 100644
index 000000000..6399a6094
--- /dev/null
+++ b/src/main/antora/modules/ROOT/assets/images/epub-cover.svg
@@ -0,0 +1,12 @@
+
+
+
diff --git a/src/main/antora/modules/ROOT/assets/images/jconsole.png b/src/main/antora/modules/ROOT/assets/images/jconsole.png
new file mode 100644
index 000000000..267aee8a4
Binary files /dev/null and b/src/main/antora/modules/ROOT/assets/images/jconsole.png differ
diff --git a/src/main/antora/modules/ROOT/examples/MongoApplication.java b/src/main/antora/modules/ROOT/examples/MongoApplication.java
new file mode 100644
index 000000000..b7cb8d3ff
--- /dev/null
+++ b/src/main/antora/modules/ROOT/examples/MongoApplication.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2023 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+// tag::file[]
+package org.springframework.data.mongodb.example;
+
+import static org.springframework.data.mongodb.core.query.Criteria.where;
+import static org.springframework.data.mongodb.core.query.Query.query;
+
+import org.springframework.data.mongodb.core.MongoOperations;
+import org.springframework.data.mongodb.core.MongoTemplate;
+import org.springframework.data.mongodb.core.query.Query;
+
+import com.mongodb.client.MongoClients;
+
+public class MongoApplication {
+
+ public static void main(String[] args) throws Exception {
+
+ MongoOperations mongoOps = new MongoTemplate(MongoClients.create(), "database");
+ mongoOps.insert(new Person("Joe", 34));
+
+ System.out.println(mongoOps.query(Person.class).matching(where("name").is("Joe")).firstValue());
+
+ mongoOps.dropCollection("person");
+ }
+}
+//end::file[]
diff --git a/src/main/antora/modules/ROOT/examples/Person.java b/src/main/antora/modules/ROOT/examples/Person.java
new file mode 100644
index 000000000..69ec1167f
--- /dev/null
+++ b/src/main/antora/modules/ROOT/examples/Person.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2023 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+// tag::file[]
+package org.springframework.data.mongodb.example;
+
+// tag::class[]
+public class Person {
+
+ private String id;
+ private String name;
+ private int age;
+
+ public Person(String name, int age) {
+ this.name = name;
+ this.age = age;
+ }
+
+ public String getId() {
+ return id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getAge() {
+ return age;
+ }
+
+ @Override
+ public String toString() {
+ return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
+ }
+}
+// end::class[]
+// end::file[]
diff --git a/src/main/antora/modules/ROOT/examples/ReactiveMongoApplication.java b/src/main/antora/modules/ROOT/examples/ReactiveMongoApplication.java
new file mode 100644
index 000000000..cbb53c590
--- /dev/null
+++ b/src/main/antora/modules/ROOT/examples/ReactiveMongoApplication.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2023 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+// tag::file[]
+package org.springframework.data.mongodb.example;
+
+import static org.springframework.data.mongodb.core.query.Criteria.*;
+import static org.springframework.data.mongodb.core.query.Query.*;
+
+import org.springframework.data.mongodb.core.ReactiveMongoOperations;
+import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
+
+import com.mongodb.reactivestreams.client.MongoClients;
+
+public class ReactiveMongoApplication {
+
+ public static void main(String[] args) throws Exception {
+
+ ReactiveMongoOperations mongoOps = new ReactiveMongoTemplate(MongoClients.create(), "database");
+
+ mongoOps.insert(new Person("Joe", 34))
+ .then(mongoOps.query(Person.class).matching(where("name").is("Joe")).first())
+ .doOnNext(System.out::println)
+ .block();
+
+ mongoOps.dropCollection("person").block();
+ }
+}
+// end::file[]
diff --git a/src/main/antora/modules/ROOT/nav.adoc b/src/main/antora/modules/ROOT/nav.adoc
new file mode 100644
index 000000000..702566fdb
--- /dev/null
+++ b/src/main/antora/modules/ROOT/nav.adoc
@@ -0,0 +1,63 @@
+* xref:index.adoc[Overview]
+** xref:commons/upgrade.adoc[]
+** xref:migration-guides.adoc[]
+*** xref:migration-guide/migration-guide-2.x-to-3.x.adoc[]
+*** xref:migration-guide/migration-guide-3.x-to-4.x.adoc[]
+
+* xref:mongodb.adoc[]
+** xref:mongodb/getting-started.adoc[]
+** xref:mongodb/configuration.adoc[]
+** xref:mongodb/template-api.adoc[]
+*** xref:mongodb/template-config.adoc[]
+*** xref:mongodb/template-crud-operations.adoc[]
+*** xref:mongodb/template-query-operations.adoc[]
+**** xref:mongodb/template-query-options.adoc[]
+*** xref:mongodb/template-document-count.adoc[]
+*** xref:mongodb/aggregation-framework.adoc[]
+*** xref:mongodb/template-collection-management.adoc[]
+**** xref:mongodb/template-collection-schema.adoc[]
+*** xref:mongodb/template-gridfs.adoc[]
+** xref:mongodb/mapping/mapping.adoc[]
+*** xref:mongodb/mapping/custom-conversions.adoc[Type based Converter]
+*** xref:mongodb/mapping/property-converters.adoc[]
+*** xref:mongodb/mapping/unwrapping-entities.adoc[]
+*** xref:mongodb/mapping/document-references.adoc[Object References]
+*** xref:mongodb/mapping/entity-callbacks.adoc[Entity Callbacks]
+*** xref:mongodb/mapping/lifecycle-events.adoc[]
+*** xref:mongodb/mapping/mapping-index-management.adoc[]
+** xref:mongodb/client-session-transactions.adoc[]
+** xref:mongodb/change-streams.adoc[]
+** xref:mongodb/tailable-cursors.adoc[]
+** xref:mongodb/sharding.adoc[]
+** xref:mongodb/mongo-encryption.adoc[]
+** xref:mongodb/auditing.adoc[]
+
+// Repository
+* xref:repositories.adoc[]
+** xref:repositories/core-concepts.adoc[]
+** xref:repositories/definition.adoc[]
+** xref:mongodb/repositories/repositories.adoc[]
+*** xref:mongodb/repositories/repositories-index-hints.adoc[]
+*** xref:mongodb/repositories/repositories-collation.adoc[]
+** xref:repositories/create-instances.adoc[]
+** xref:repositories/query-methods-details.adoc[]
+** xref:repositories/projections.adoc[]
+** xref:repositories/custom-implementations.adoc[]
+** xref:repositories/core-domain-events.adoc[]
+** xref:repositories/null-handling.adoc[]
+** xref:mongodb/repositories/cdi-integration.adoc[]
+** xref:repositories/query-keywords-reference.adoc[]
+** xref:repositories/query-return-types-reference.adoc[]
+
+// Observability
+* xref:observability/observability.adoc[]
+** xref:observability/conventions.adoc[]
+** xref:observability/metrics.adoc[]
+** xref:observability/spans.adoc[]
+
+* xref:kotlin.adoc[]
+** xref:kotlin/requirements.adoc[]
+** xref:kotlin/null-safety.adoc[]
+** xref:kotlin/extensions.adoc[]
+** xref:kotlin/coroutines.adoc[]
+
diff --git a/src/main/antora/modules/ROOT/pages/commons/upgrade.adoc b/src/main/antora/modules/ROOT/pages/commons/upgrade.adoc
new file mode 100644
index 000000000..51a9189aa
--- /dev/null
+++ b/src/main/antora/modules/ROOT/pages/commons/upgrade.adoc
@@ -0,0 +1 @@
+include::{commons}@data-commons::page$upgrade.adoc[]
diff --git a/src/main/antora/modules/ROOT/pages/index.adoc b/src/main/antora/modules/ROOT/pages/index.adoc
new file mode 100644
index 000000000..5c385f351
--- /dev/null
+++ b/src/main/antora/modules/ROOT/pages/index.adoc
@@ -0,0 +1,22 @@
+[[spring-data-mongodb-reference-documentation]]
+= Spring Data MongoDB
+:revnumber: {version}
+:revdate: {localdate}
+:feature-scroll: true
+
+_Spring Data MongoDB provides support for the MongoDB database.
+It uses familiar Spring concepts such as a template classes for core API usage and lightweight repository style data access to ease development of applications with a consistent programming model._
+
+[horizontal]
+xref:mongodb.adoc[MongoDB] :: MongoDB support and connectivity
+xref:repositories.adoc[Repositories] :: Mongo Repositories
+xref:observability/observability.adoc[Observability] :: Observability Integration
+xref:kotlin.adoc[Kotlin] :: Kotlin support
+// xref:migration-guides.adoc[Migration] :: Migration Guides
+https://github.com/spring-projects/spring-data-commons/wiki[Wiki] :: What's New, Upgrade Notes, Supported Versions, additional cross-version information.
+
+Mark Pollack; Thomas Risberg; Oliver Gierke; Costin Leau; Jon Brisbin; Thomas Darimont; Christoph Strobl; Mark Paluch; Jay Bryant
+
+(C) 2008-2022 VMware Inc.
+
+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.
diff --git a/src/main/antora/modules/ROOT/pages/kotlin.adoc b/src/main/antora/modules/ROOT/pages/kotlin.adoc
new file mode 100644
index 000000000..4f01678d8
--- /dev/null
+++ b/src/main/antora/modules/ROOT/pages/kotlin.adoc
@@ -0,0 +1 @@
+include::{commons}@data-commons::page$kotlin.adoc[]
diff --git a/src/main/antora/modules/ROOT/pages/kotlin/coroutines.adoc b/src/main/antora/modules/ROOT/pages/kotlin/coroutines.adoc
new file mode 100644
index 000000000..8f578961c
--- /dev/null
+++ b/src/main/antora/modules/ROOT/pages/kotlin/coroutines.adoc
@@ -0,0 +1 @@
+include::{commons}@data-commons::page$kotlin/coroutines.adoc[]
diff --git a/src/main/antora/modules/ROOT/pages/kotlin/extensions.adoc b/src/main/antora/modules/ROOT/pages/kotlin/extensions.adoc
new file mode 100644
index 000000000..6ad73541a
--- /dev/null
+++ b/src/main/antora/modules/ROOT/pages/kotlin/extensions.adoc
@@ -0,0 +1,65 @@
+include::{commons}@data-commons::page$kotlin/extensions.adoc[]
+
+To retrieve a list of `SWCharacter` objects in Java, you would normally write the following:
+
+[source,java]
+----
+Flux characters = template.query(SWCharacter.class).inTable("star-wars").all()
+----
+
+With Kotlin and the Spring Data extensions, you can instead write the following:
+
+[source,kotlin]
+----
+val characters = template.query().inTable("star-wars").all()
+// or (both are equivalent)
+val characters : Flux = template.query().inTable("star-wars").all()
+----
+
+As in Java, `characters` in Kotlin is strongly typed, but Kotlin's clever type inference allows for shorter syntax.
+
+[[mongo.query.kotlin-support]]
+== Type-safe Queries for Kotlin
+
+Kotlin embraces domain-specific language creation through its language syntax and its extension system.
+Spring Data MongoDB ships with a Kotlin Extension for `Criteria` using https://kotlinlang.org/docs/reference/reflection.html#property-references[Kotlin property references] to build type-safe queries.
+Queries using this extension are typically benefit from improved readability.
+Most keywords on `Criteria` have a matching Kotlin extension, such as `inValues` and `regex`.
+
+Consider the following example explaining Type-safe Queries:
+
+====
+[source,kotlin]
+----
+import org.springframework.data.mongodb.core.query.*
+
+mongoOperations.find(
+ Query(Book::title isEqualTo "Moby-Dick") <1>
+)
+
+mongoOperations.find(
+ Query(titlePredicate = Book::title exists true)
+)
+
+mongoOperations.find(
+ Query(
+ Criteria().andOperator(
+ Book::price gt 5,
+ Book::price lt 10
+ ))
+)
+
+// Binary operators
+mongoOperations.find(
+ Query(BinaryMessage::payload bits { allClear(0b101) }) <2>
+)
+
+// Nested Properties (i.e. refer to "book.author")
+mongoOperations.find(
+ Query(Book::author / Author::name regex "^H") <3>
+)
+----
+<1> `isEqualTo()` is an infix extension function with receiver type `KProperty` that returns `Criteria`.
+<2> For bitwise operators, pass a lambda argument where you call one of the methods of `Criteria.BitwiseCriteriaOperators`.
+<3> To construct nested properties, use the `/` character (overloaded operator `div`).
+====
diff --git a/src/main/antora/modules/ROOT/pages/kotlin/null-safety.adoc b/src/main/antora/modules/ROOT/pages/kotlin/null-safety.adoc
new file mode 100644
index 000000000..6967ddb3f
--- /dev/null
+++ b/src/main/antora/modules/ROOT/pages/kotlin/null-safety.adoc
@@ -0,0 +1 @@
+include::{commons}@data-commons::page$kotlin/null-safety.adoc[]
diff --git a/src/main/antora/modules/ROOT/pages/kotlin/object-mapping.adoc b/src/main/antora/modules/ROOT/pages/kotlin/object-mapping.adoc
new file mode 100644
index 000000000..76cab88e9
--- /dev/null
+++ b/src/main/antora/modules/ROOT/pages/kotlin/object-mapping.adoc
@@ -0,0 +1 @@
+// include::{commons}@data-commons::page$kotlin/object-mapping.adoc[]
diff --git a/src/main/antora/modules/ROOT/pages/kotlin/requirements.adoc b/src/main/antora/modules/ROOT/pages/kotlin/requirements.adoc
new file mode 100644
index 000000000..bb209ab6a
--- /dev/null
+++ b/src/main/antora/modules/ROOT/pages/kotlin/requirements.adoc
@@ -0,0 +1 @@
+include::{commons}@data-commons::page$kotlin/requirements.adoc[]
diff --git a/src/main/asciidoc/reference/migrating.adoc b/src/main/antora/modules/ROOT/pages/migration-guide/migration-guide-2.x-to-3.x.adoc
similarity index 80%
rename from src/main/asciidoc/reference/migrating.adoc
rename to src/main/antora/modules/ROOT/pages/migration-guide/migration-guide-2.x-to-3.x.adoc
index 0a8298487..c002c1fee 100644
--- a/src/main/asciidoc/reference/migrating.adoc
+++ b/src/main/antora/modules/ROOT/pages/migration-guide/migration-guide-2.x-to-3.x.adoc
@@ -1,12 +1,11 @@
-[[migrating]]
-= Migrating
+[[mongodb.migration.2.x-3.x]]
+= Migration Guide from 2.x to 3.x
-This chapter coverts major changes and outlines migration steps.
+Spring Data MongoDB 3.x requires the MongoDB Java Driver 4.x +
+To learn more about driver versions please visit the https://www.mongodb.com/docs/drivers/java/sync/current/upgrade/[MongoDB Documentation].
-[[migrating-2.x-to-3.0]]
-== 2.x to 3.0
-
-=== Dependency Changes
+[[dependency-changes]]
+== Dependency Changes
* `org.mongodb:mongo-java-driver` (uber jar) got replaced with:
** bson-jar
@@ -16,25 +15,29 @@ This chapter coverts major changes and outlines migration steps.
The change in dependencies allows usage of the reactive support without having to pull the synchronous driver.
NOTE: The new sync driver does no longer support `com.mongodb.DBObject`. Please use `org.bson.Document` instead.
-=== Signature Changes
+[[signature-changes]]
+== Signature Changes
* `MongoTemplate` no longer supports `com.mongodb.MongoClient` and `com.mongodb.MongoClientOptions`.
Please use `com.mongodb.client.MongoClient` and `com.mongodb.MongoClientSettings` instead.
In case you're using `AbstractMongoConfiguration` please switch to `AbstractMongoClientConfiguration`.
-=== Namespace Changes
+[[namespace-changes]]
+== Namespace Changes
The switch to `com.mongodb.client.MongoClient` requires an update of your configuration XML if you have one.
The best way to provide required connection information is by using a connection string.
Please see the https://docs.mongodb.com/manual/reference/connection-string/[MongoDB Documentation] for details.
-[source,xml]
+
====
+[source,xml]
----
----
+[source,xml]
----
@@ -45,6 +48,7 @@ Please see the https://docs.mongodb.com/manual/reference/connection-string/[Mong
connection-string="mongodb://${mongo.host}:${mongo.port}/?replicaSet=rs0" />
----
+[source,xml]
----
----
====
-
-
-
diff --git a/src/main/asciidoc/upgrading.adoc b/src/main/antora/modules/ROOT/pages/migration-guide/migration-guide-3.x-to-4.x.adoc
similarity index 57%
rename from src/main/asciidoc/upgrading.adoc
rename to src/main/antora/modules/ROOT/pages/migration-guide/migration-guide-3.x-to-4.x.adoc
index b710c5bfb..aa340a4f2 100644
--- a/src/main/asciidoc/upgrading.adoc
+++ b/src/main/antora/modules/ROOT/pages/migration-guide/migration-guide-3.x-to-4.x.adoc
@@ -1,10 +1,5 @@
-[[upgrading.data-mongo]]
-= Upgrading
-
-include::{spring-data-commons-docs}/upgrade.adoc[leveloffset=+1]
-
-[[upgrading.3-4]]
-== Upgrading MongoDB Drivers
+[[mongodb.migration.3.x-4.x]]
+= Migration Guide from 3.x to 4.x
Spring Data MongoDB 4.x requires the MongoDB Java Driver 4.8.x +
To learn more about driver versions please visit the https://www.mongodb.com/docs/drivers/java/sync/current/upgrade/[MongoDB Documentation].
diff --git a/src/main/antora/modules/ROOT/pages/migration-guides.adoc b/src/main/antora/modules/ROOT/pages/migration-guides.adoc
new file mode 100644
index 000000000..3f8e783a1
--- /dev/null
+++ b/src/main/antora/modules/ROOT/pages/migration-guides.adoc
@@ -0,0 +1,8 @@
+[[mongodb.migration]]
+= Migration Guides
+:page-section-summary-toc: 1
+
+This section contains version-specific migration guides explaining how to upgrade between two versions.
+
+
+
diff --git a/src/main/antora/modules/ROOT/pages/mongodb.adoc b/src/main/antora/modules/ROOT/pages/mongodb.adoc
new file mode 100644
index 000000000..eebf9a914
--- /dev/null
+++ b/src/main/antora/modules/ROOT/pages/mongodb.adoc
@@ -0,0 +1,22 @@
+[[mongodb.core]]
+= MongoDB Support
+:page-section-summary-toc: 1
+
+Spring Data support for MongoDB 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.
+* `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.
+* Annotation-based mapping metadata that is extensible to support other metadata formats.
+* Persistence and mapping lifecycle events.
+* Java-based Query, Criteria, and Update DSLs.
+* Automatic implementation of Repository interfaces, including support for custom finder methods.
+* QueryDSL integration to support type-safe queries.
+* Multi Document Transactions.
+* GeoSpatial integration.
+
+For most tasks, you should use `MongoTemplate` or the Repository support, which both leverage the rich mapping functionality.
+`MongoTemplate` is the place to look for accessing functionality such as incrementing counters or ad-hoc CRUD operations.
+`MongoTemplate` also provides callback methods so that it is easy for you to get the low-level API artifacts, such as `com.mongodb.client.MongoDatabase`, to communicate directly with MongoDB.
+The goal with naming conventions on various API artifacts is to copy those in the base MongoDB Java driver so you can easily map your existing knowledge onto the Spring APIs.
diff --git a/src/main/asciidoc/reference/aggregation-framework.adoc b/src/main/antora/modules/ROOT/pages/mongodb/aggregation-framework.adoc
similarity index 94%
rename from src/main/asciidoc/reference/aggregation-framework.adoc
rename to src/main/antora/modules/ROOT/pages/mongodb/aggregation-framework.adoc
index c2107e94d..3f34e55ae 100644
--- a/src/main/asciidoc/reference/aggregation-framework.adoc
+++ b/src/main/antora/modules/ROOT/pages/mongodb/aggregation-framework.adoc
@@ -1,12 +1,12 @@
[[mongo.aggregation]]
-== Aggregation Framework Support
+= Aggregation Framework Support
Spring Data MongoDB provides support for the Aggregation Framework introduced to MongoDB in version 2.2.
For further information, see the full https://docs.mongodb.org/manual/aggregation/[reference documentation] of the aggregation framework and other data aggregation tools for MongoDB.
[[mongo.aggregation.basic-concepts]]
-=== Basic Concepts
+== Basic Concepts
The Aggregation Framework support in Spring Data MongoDB is based on the following key abstractions: `Aggregation`, `AggregationDefinition`, and `AggregationResults`.
@@ -52,12 +52,12 @@ List mappedResult = results.getMappedResults();
Note that, if you provide an input class as the first parameter to the `newAggregation` method, the `MongoTemplate` derives the name of the input collection from this class. Otherwise, if you do not not specify an input class, you must provide the name of the input collection explicitly. If both an input class and an input collection are provided, the latter takes precedence.
[[mongo.aggregation.supported-aggregation-operations]]
-=== Supported Aggregation Operations & Stages
-
+[[aggregation-stages]]
+.Supported Aggregation Operations & Stages
+[%collapsible]
+====
The MongoDB Aggregation Framework provides the following types of aggregation stages and operations:
-==== Aggregation Stages
-
* addFields - `AddFieldsOperation`
* bucket / bucketAuto - `BucketOperation` / `BucketAutoOperation`
* count - `CountOperation`
@@ -81,6 +81,7 @@ The MongoDB Aggregation Framework provides the following types of aggregation st
* unionWith - `UnionWithOperation`
* unset - `UnsetOperation`
* unwind - `UnwindOperation`
+====
[TIP]
====
@@ -102,21 +103,6 @@ Aggregation.stage("""
----
====
-==== Aggregation Operators
-
-* Group/Accumulator Aggregation Operators
-* Boolean Aggregation Operators
-* Comparison Aggregation Operators
-* Arithmetic Aggregation Operators
-* String Aggregation Operators
-* Date Aggregation Operators
-* Array Aggregation Operators
-* Conditional Aggregation Operators
-* Lookup Aggregation Operators
-* Convert Aggregation Operators
-* Object Aggregation Operators
-* Script Aggregation Operators
-
At the time of this writing, we provide support for the following Aggregation Operators in Spring Data MongoDB:
.Aggregation Operators currently supported by Spring Data MongoDB
@@ -171,7 +157,7 @@ At the time of this writing, we provide support for the following Aggregation Op
Note that the aggregation operations not listed here are currently not supported by Spring Data MongoDB. Comparison aggregation operators are expressed as `Criteria` expressions.
[[mongo.aggregation.projection]]
-=== Projection Expressions
+== Projection Expressions
Projection expressions are used to define the fields that are the outcome of a particular aggregation step. Projection expressions can be defined through the `project` method of the `Aggregation` class, either by passing a list of `String` objects or an aggregation framework `Fields` object. The projection can be extended with additional fields through a fluent API by using the `and(String)` method and aliased by using the `as(String)` method.
Note that you can also define fields with aliases by using the `Fields.field` static factory method of the aggregation framework, which you can then use to construct a new `Fields` instance. References to projected fields in later aggregation stages are valid only for the field names of included fields or their aliases (including newly defined fields and their aliases). Fields not included in the projection cannot be referenced in later aggregation stages. The following listings show examples of projection expression:
@@ -209,11 +195,12 @@ project().and("firstname").as("name"), sort(ASC, "firstname")
More examples for project operations can be found in the `AggregationTests` class. Note that further details regarding the projection expressions can be found in the https://docs.mongodb.org/manual/reference/operator/aggregation/project/#pipe._S_project[corresponding section] of the MongoDB Aggregation Framework reference documentation.
[[mongo.aggregation.facet]]
-=== Faceted Classification
+== Faceted Classification
As of Version 3.4, MongoDB supports faceted classification by using the Aggregation Framework. A faceted classification uses semantic categories (either general or subject-specific) that are combined to create the full classification entry. Documents flowing through the aggregation pipeline are classified into buckets. A multi-faceted classification enables various aggregations on the same set of input documents, without needing to retrieve the input documents multiple times.
-==== Buckets
+[[buckets]]
+=== 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 the operation by using the `as(String)` method. Each bucket is represented as a document in the output.
@@ -254,12 +241,13 @@ bucketAuto("price", 5).andOutput("title").push().as("titles");
----
====
-To create output fields in buckets, bucket operations can use `AggregationExpression` through `andOutput()` and <> through `andOutputExpression()`.
+To create output fields in buckets, bucket operations can use `AggregationExpression` through `andOutput()` and xref:mongodb/aggregation-framework.adoc#mongo.aggregation.projection.expressions[SpEL expressions] through `andOutputExpression()`.
Note that further details regarding bucket expressions can be found in the https://docs.mongodb.org/manual/reference/operator/aggregation/bucket/[`$bucket` section] and
https://docs.mongodb.org/manual/reference/operator/aggregation/bucketAuto/[`$bucketAuto` section] of the MongoDB Aggregation Framework reference documentation.
-==== Multi-faceted Aggregation
+[[multi-faceted-aggregation]]
+=== Multi-faceted Aggregation
Multiple aggregation pipelines can be used to create multi-faceted aggregations that characterize data across multiple dimensions (or facets) within a single aggregation stage. Multi-faceted aggregations provide multiple filters and categorizations to guide data browsing and analysis. A common implementation of faceting is how many online retailers provide ways to narrow down search results by applying filters on product price, manufacturer, size, and other factors.
@@ -290,9 +278,9 @@ facet(project("title").and("publicationDate").extractYear().as("publicationYear"
Note that further details regarding facet operation can be found in the https://docs.mongodb.org/manual/reference/operator/aggregation/facet/[`$facet` section] of the MongoDB Aggregation Framework reference documentation.
[[mongo.aggregation.sort-by-count]]
-==== Sort By Count
+=== Sort By Count
-Sort by count operations group incoming documents based on the value of a specified expression, compute the count of documents in each distinct group, and sort the results by count. It offers a handy shortcut to apply sorting when using <>. Sort by count operations require a grouping field or grouping expression. The following listing shows a sort by count example:
+Sort by count operations group incoming documents based on the value of a specified expression, compute the count of documents in each distinct group, and sort the results by count. It offers a handy shortcut to apply sorting when using xref:mongodb/aggregation-framework.adoc#mongo.aggregation.facet[Faceted Classification]. Sort by count operations require a grouping field or grouping expression. The following listing shows a sort by count example:
.Sort by count example
====
@@ -311,11 +299,12 @@ A sort by count operation is equivalent to the following BSON (Binary JSON):
----
[[mongo.aggregation.projection.expressions]]
-==== Spring Expression Support in Projection Expressions
+=== Spring Expression Support in Projection Expressions
We support the use of SpEL expressions in projection expressions through the `andExpression` method of the `ProjectionOperation` and `BucketOperation` classes. This feature lets you define the desired expression as a SpEL expression. On running a query, the SpEL expression is translated into a corresponding MongoDB projection expression part. This arrangement makes it much easier to express complex calculations.
-===== Complex Calculations with SpEL expressions
+[[complex-calculations-with-spel-expressions]]
+==== Complex Calculations with SpEL expressions
Consider the following SpEL expression:
@@ -336,9 +325,12 @@ The preceding expression is translated into the following projection expression
}]}
----
-You can see examples in more context in <> and <>. You can find more usage examples for supported SpEL expression constructs in `SpelExpressionTransformerUnitTests`. The following table shows the SpEL transformations supported by Spring Data MongoDB:
+You can see examples in more context in xref:mongodb/aggregation-framework.adoc#mongo.aggregation.examples.example5[Aggregation Framework Example 5] and xref:mongodb/aggregation-framework.adoc#mongo.aggregation.examples.example6[Aggregation Framework Example 6].
+You can find more usage examples for supported SpEL expression constructs in `SpelExpressionTransformerUnitTests`.
.Supported SpEL transformations
+[%collapsible]
+====
[%header,cols="2"]
|===
| SpEL Expression
@@ -374,6 +366,7 @@ You can see examples in more context in <>
| !a
| { $not : [$a] }
|===
+====
In addition to the transformations shown in the preceding table, you can use standard SpEL operations such as `new` to (for example) create arrays and reference expressions through their names (followed by the arguments to use in brackets). The following example shows how to create an array in this fashion:
@@ -384,12 +377,12 @@ In addition to the transformations shown in the preceding table, you can use sta
----
[[mongo.aggregation.examples]]
-==== Aggregation Framework Examples
+=== Aggregation Framework Examples
The examples in this section demonstrate the usage patterns for the MongoDB Aggregation Framework with Spring Data MongoDB.
[[mongo.aggregation.examples.example1]]
-===== Aggregation Framework Example 1
+==== Aggregation Framework Example 1
In this introductory example, we want to aggregate a list of tags to get the occurrence count of a particular tag from a MongoDB collection (called `tags`) sorted by the occurrence count in descending order. This example demonstrates the usage of grouping, sorting, projections (selection), and unwinding (result splitting).
@@ -430,7 +423,7 @@ The preceding listing uses the following algorithm:
Note that the input collection is explicitly specified as the `tags` parameter to the `aggregate` Method. If the name of the input collection is not specified explicitly, it is derived from the input class passed as the first parameter to the `newAggreation` method.
[[mongo.aggregation.examples.example2]]
-===== Aggregation Framework Example 2
+==== Aggregation Framework Example 2
This example is based on the https://docs.mongodb.org/manual/tutorial/aggregation-examples/#largest-and-smallest-cities-by-state[Largest and Smallest Cities by State] example from the MongoDB Aggregation Framework documentation. We added additional sorting to produce stable results with different MongoDB versions. Here we want to return the smallest and largest cities by population for each state by using the aggregation framework. This example demonstrates grouping, sorting, and projections (selection).
@@ -496,7 +489,7 @@ The preceding listings use the following algorithm:
Note that we derive the name of the input collection from the `ZipInfo` class passed as the first parameter to the `newAggregation` method.
[[mongo.aggregation.examples.example3]]
-===== Aggregation Framework Example 3
+==== Aggregation Framework Example 3
This example is based on the https://docs.mongodb.org/manual/tutorial/aggregation-examples/#states-with-populations-over-10-million[States with Populations Over 10 Million] example from the MongoDB Aggregation Framework documentation. We added additional sorting to produce stable results with different MongoDB versions. Here we want to return all states with a population greater than 10 million, using the aggregation framework. This example demonstrates grouping, sorting, and matching (filtering).
@@ -532,7 +525,7 @@ The preceding listings use the following algorithm:
Note that we derive the name of the input collection from the `ZipInfo` class passed as first parameter to the `newAggregation` method.
[[mongo.aggregation.examples.example4]]
-===== Aggregation Framework Example 4
+==== Aggregation Framework Example 4
This example demonstrates the use of simple arithmetic operations in the projection operation.
@@ -566,7 +559,7 @@ List resultList = result.getMappedResults();
Note that we derive the name of the input collection from the `Product` class passed as first parameter to the `newAggregation` method.
[[mongo.aggregation.examples.example5]]
-===== Aggregation Framework Example 5
+==== Aggregation Framework Example 5
This example demonstrates the use of simple arithmetic operations derived from SpEL Expressions in the projection operation.
@@ -600,7 +593,7 @@ List resultList = result.getMappedResults();
----
[[mongo.aggregation.examples.example6]]
-===== Aggregation Framework Example 6
+==== Aggregation Framework Example 6
This example demonstrates the use of complex arithmetic operations derived from SpEL Expressions in the projection operation.
@@ -634,7 +627,7 @@ List resultList = result.getMappedResults();
Note that we can also refer to other fields of the document within the SpEL expression.
[[mongo.aggregation.examples.example7]]
-===== Aggregation Framework Example 7
+==== Aggregation Framework Example 7
This example uses conditional projection. It is derived from the https://docs.mongodb.com/manual/reference/operator/aggregation/cond/[$cond reference documentation].
diff --git a/src/main/antora/modules/ROOT/pages/mongodb/auditing.adoc b/src/main/antora/modules/ROOT/pages/mongodb/auditing.adoc
new file mode 100644
index 000000000..014dde02c
--- /dev/null
+++ b/src/main/antora/modules/ROOT/pages/mongodb/auditing.adoc
@@ -0,0 +1,47 @@
+[[mongo.auditing]]
+= Auditing
+
+Since Spring Data MongoDB 1.4, auditing can be enabled by annotating a configuration class with the `@EnableMongoAuditing` annotation, as the following example shows:
+
+[tabs]
+======
+Imperative::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
+----
+@Configuration
+@EnableMongoAuditing
+class Config {
+
+ @Bean
+ public AuditorAware myAuditorProvider() {
+ return new AuditorAwareImpl();
+ }
+}
+----
+
+Reactive::
++
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+@Configuration
+@EnableReactiveMongoAuditing
+class Config {
+
+ @Bean
+ public ReactiveAuditorAware myAuditorProvider() {
+ return new ReactiveAuditorAwareImpl();
+ }
+}
+----
+
+XML::
++
+[source,xml,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+
+----
+======
+
+If you expose a bean of type `AuditorAware` / `ReactiveAuditorAware` to the `ApplicationContext`, the auditing infrastructure picks it up automatically and uses it to determine the current user to be set on domain types.
+If you have multiple implementations registered in the `ApplicationContext`, you can select the one to be used by explicitly setting the `auditorAwareRef` attribute of `@EnableMongoAuditing`.
diff --git a/src/main/asciidoc/reference/change-streams.adoc b/src/main/antora/modules/ROOT/pages/mongodb/change-streams.adoc
similarity index 96%
rename from src/main/asciidoc/reference/change-streams.adoc
rename to src/main/antora/modules/ROOT/pages/mongodb/change-streams.adoc
index aff9db2d2..ed2e169c9 100644
--- a/src/main/asciidoc/reference/change-streams.adoc
+++ b/src/main/antora/modules/ROOT/pages/mongodb/change-streams.adoc
@@ -1,5 +1,5 @@
[[change-streams]]
-== Change Streams
+= Change Streams
As of MongoDB 3.6, https://docs.mongodb.com/manual/changeStreams/[Change Streams] let applications get notified about changes without having to tail the oplog.
@@ -12,7 +12,8 @@ changes from all collections within the database. When subscribing to a database
suitable type for the event type as conversion might not apply correctly across different entity types.
In doubt, use `Document`.
-=== Change Streams with `MessageListener`
+[[change-streams-with-messagelistener]]
+== Change Streams with `MessageListener`
Listening to a https://docs.mongodb.com/manual/tutorial/change-streams-example/[Change Stream by using a Sync Driver] creates a long running, blocking task that needs to be delegated to a separate component.
In this case, we need to first create a `MessageListenerContainer`, which will be the main entry point for running the specific `SubscriptionRequest` tasks.
@@ -49,7 +50,8 @@ Errors while processing are passed on to an `org.springframework.util.ErrorHandl
Please use `register(request, body, errorHandler)` to provide additional functionality.
====
-=== Reactive Change Streams
+[[reactive-change-streams]]
+== Reactive Change Streams
Subscribing to Change Streams with the reactive API is a more natural approach to work with streams. Still, the essential building blocks, such as `ChangeStreamOptions`, remain the same. The following example shows how to use Change Streams emitting ``ChangeStreamEvent``s:
@@ -67,7 +69,8 @@ Flux> flux = reactiveTemplate.changeStream(User.class) <
<3> Obtain a `Flux` of change stream events. The `ChangeStreamEvent#getBody()` is converted to the requested domain type from (2).
====
-=== Resuming Change Streams
+[[resuming-change-streams]]
+== Resuming Change Streams
Change Streams can be resumed and resume emitting events where you left. To resume the stream, you need to supply either a resume
token or the last known server time (in UTC). Use `ChangeStreamOptions` to set the value accordingly.
diff --git a/src/main/asciidoc/reference/client-session-transactions.adoc b/src/main/antora/modules/ROOT/pages/mongodb/client-session-transactions.adoc
similarity index 78%
rename from src/main/asciidoc/reference/client-session-transactions.adoc
rename to src/main/antora/modules/ROOT/pages/mongodb/client-session-transactions.adoc
index 0626956c6..f623067c7 100644
--- a/src/main/asciidoc/reference/client-session-transactions.adoc
+++ b/src/main/antora/modules/ROOT/pages/mongodb/client-session-transactions.adoc
@@ -1,22 +1,31 @@
[[mongo.sessions]]
-= MongoDB Sessions
+= Sessions & Transactions
-As of version 3.6, MongoDB supports the concept of sessions. The use of sessions enables MongoDB's https://docs.mongodb.com/manual/core/read-isolation-consistency-recency/#causal-consistency[Causal Consistency] model, which guarantees running operations in an order that respects their causal relationships. Those are split into `ServerSession` instances and `ClientSession` instances. In this section, when we speak of a session, we refer to `ClientSession`.
+As of version 3.6, MongoDB supports the concept of sessions.
+The use of sessions enables MongoDB's https://docs.mongodb.com/manual/core/read-isolation-consistency-recency/#causal-consistency[Causal Consistency] model, which guarantees running operations in an order that respects their causal relationships.
+Those are split into `ServerSession` instances and `ClientSession` instances.
+In this section, when we speak of a session, we refer to `ClientSession`.
WARNING: Operations within a client session are not isolated from operations outside the session.
-Both `MongoOperations` and `ReactiveMongoOperations` provide gateway methods for tying a `ClientSession` to the operations. `MongoCollection` and `MongoDatabase` use session proxy objects that implement MongoDB's collection and database interfaces, so you need not add a session on each call. This means that a potential call to `MongoCollection#find()` is delegated to `MongoCollection#find(ClientSession)`.
+Both `MongoOperations` and `ReactiveMongoOperations` provide gateway methods for tying a `ClientSession` to the operations.
+`MongoCollection` and `MongoDatabase` use session proxy objects that implement MongoDB's collection and database interfaces, so you need not add a session on each call.
+This means that a potential call to `MongoCollection#find()` is delegated to `MongoCollection#find(ClientSession)`.
NOTE: Methods such as `(Reactive)MongoOperations#getCollection` return native MongoDB Java Driver gateway objects (such as `MongoCollection`) that themselves offer dedicated methods for `ClientSession`. These methods are *NOT* session-proxied. You should provide the `ClientSession` where needed when interacting directly with a `MongoCollection` or `MongoDatabase` and not through one of the `#execute` callbacks on `MongoOperations`.
[[mongo.sessions.sync]]
-== Synchronous `ClientSession` support.
+[[mongo.sessions.reactive]]
+== ClientSession support
The following example shows the usage of a session:
-.`ClientSession` with `MongoOperations`
+[tabs]
+======
+Imperative::
++
====
-[source,java]
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
----
ClientSessionOptions sessionOptions = ClientSessionOptions.builder()
.causallyConsistent(true)
@@ -44,27 +53,23 @@ session.close() <4>
<2> Use `MongoOperation` methods as before. The `ClientSession` gets applied automatically.
<3> Make sure to close the `ClientSession`.
<4> Close the session.
-====
WARNING: When dealing with `DBRef` instances, especially lazily loaded ones, it is essential to *not* close the `ClientSession` before all data is loaded. Otherwise, lazy fetch fails.
-
-[[mongo.sessions.reactive]]
-== Reactive `ClientSession` support
-
-The reactive counterpart uses the same building blocks as the imperative one, as the following example shows:
-
-.ClientSession with `ReactiveMongoOperations`
====
-[source,java]
+
+Reactive::
++
+====
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
----
ClientSessionOptions sessionOptions = ClientSessionOptions.builder()
- .causallyConsistent(true)
- .build();
+.causallyConsistent(true)
+.build();
Publisher session = client.startSession(sessionOptions); <1>
template.withSession(session)
- .execute(action -> {
+.execute(action -> {
Query query = query(where("name").is("Durzo Blint"));
return action.findOne(query, Person.class)
@@ -82,28 +87,33 @@ template.withSession(session)
<2> Use `ReactiveMongoOperation` methods as before. The `ClientSession` is obtained and applied automatically.
<3> Make sure to close the `ClientSession`.
<4> Nothing happens until you subscribe. See https://projectreactor.io/docs/core/release/reference/#reactive.subscribe[the Project Reactor Reference Guide] for details.
-====
By using a `Publisher` that provides the actual session, you can defer session acquisition to the point of actual subscription.
Still, you need to close the session when done, so as to not pollute the server with stale sessions. Use the `doFinally` hook on `execute` to call `ClientSession#close()` when you no longer need the session.
If you prefer having more control over the session itself, you can obtain the `ClientSession` through the driver and provide it through a `Supplier`.
NOTE: Reactive use of `ClientSession` is limited to Template API usage. There's currently no session integration with reactive repositories.
+====
+======
[[mongo.transactions]]
-= MongoDB Transactions
+== MongoDB Transactions
-As of version 4, MongoDB supports https://www.mongodb.com/transactions[Transactions]. Transactions are built on top of <> and, consequently, require an active `ClientSession`.
+As of version 4, MongoDB supports https://www.mongodb.com/transactions[Transactions]. Transactions are built on top of xref:mongodb/client-session-transactions.adoc[Sessions] and, consequently, require an active `ClientSession`.
NOTE: Unless you specify a `MongoTransactionManager` within your application context, transaction support is *DISABLED*. You can use `setSessionSynchronization(ALWAYS)` to participate in ongoing non-native MongoDB transactions.
To get full programmatic control over transactions, you may want to use the session callback on `MongoOperations`.
-The following example shows programmatic transaction control within a `SessionCallback`:
+The following example shows programmatic transaction control:
.Programmatic transactions
+[tabs]
+======
+Imperative::
++
====
-[source,java]
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
----
ClientSession session = client.startSession(options); <1>
@@ -133,19 +143,58 @@ template.withSession(session)
<3> If everything works out as expected, commit the changes.
<4> Something broke, so roll back everything.
<5> Do not forget to close the session when done.
-====
The preceding example lets you have full control over transactional behavior while using the session scoped `MongoOperations` instance within the callback to ensure the session is passed on to every server call.
To avoid some of the overhead that comes with this approach, you can use a `TransactionTemplate` to take away some of the noise of manual transaction flow.
+====
+
+Reactive::
++
+====
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+Mono result = Mono
+ .from(client.startSession()) <1>
+
+ .flatMap(session -> {
+ session.startTransaction(); <2>
+
+ return Mono.from(collection.deleteMany(session, ...)) <3>
+
+ .onErrorResume(e -> Mono.from(session.abortTransaction()).then(Mono.error(e))) <4>
+
+ .flatMap(val -> Mono.from(session.commitTransaction()).then(Mono.just(val))) <5>
+
+ .doFinally(signal -> session.close()); <6>
+ });
+----
+<1> First we obviously need to initiate the session.
+<2> Once we have the `ClientSession` at hand, start the transaction.
+<3> Operate within the transaction by passing on the `ClientSession` to the operation.
+<4> If the operations completes exceptionally, we need to stop the transaction and preserve the error.
+<5> Or of course, commit the changes in case of success. Still preserving the operations result.
+<6> Lastly, we need to make sure to close the session.
+
+The culprit of the above operation is in keeping the main flows `DeleteResult` instead of the transaction outcome
+published via either `commitTransaction()` or `abortTransaction()`, which leads to a rather complicated setup.
+
+NOTE: Unless you specify a `ReactiveMongoTransactionManager` within your application context, transaction support is *DISABLED*. You can use `setSessionSynchronization(ALWAYS)` to participate in ongoing non-native MongoDB transactions.
+====
+======
[[mongo.transactions.transaction-template]]
-== Transactions with `TransactionTemplate`
+[[mongo.transactions.reactive-operator]]
+== Transactions with TransactionTemplate / TransactionalOperator
-Spring Data MongoDB transactions support a `TransactionTemplate`. The following example shows how to create and use a `TransactionTemplate`:
+Spring Data MongoDB transactions support both `TransactionTemplate` and `TransactionalOperator`.
-.Transactions with `TransactionTemplate`
+.Transactions with `TransactionTemplate` / `TransactionalOperator`
+[tabs]
+======
+Imperative::
++
====
-[source,java]
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
----
template.setSessionSynchronization(ALWAYS); <1>
@@ -170,19 +219,54 @@ txTemplate.execute(new TransactionCallbackWithoutResult() {
<1> Enable transaction synchronization during Template API configuration.
<2> Create the `TransactionTemplate` using the provided `PlatformTransactionManager`.
<3> Within the callback the `ClientSession` and transaction are already registered.
-====
CAUTION: Changing state of `MongoTemplate` during runtime (as you might think would be possible in item 1 of the preceding listing) can cause threading and visibility issues.
+====
+
+Reactive::
++
+====
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+template.setSessionSynchronization(ALWAYS); <1>
+
+// ...
+
+TransactionalOperator rxtx = TransactionalOperator.create(anyTxManager,
+ new DefaultTransactionDefinition()); <2>
+
+
+Step step = // ...;
+template.insert(step);
+
+Mono process(step)
+ .then(template.update(Step.class).apply(Update.set("state", …))
+ .as(rxtx::transactional) <3>
+ .then();
+----
+<1> Enable transaction synchronization for Transactional participation.
+<2> Create the `TransactionalOperator` using the provided `ReactiveTransactionManager`.
+<3> `TransactionalOperator.transactional(…)` provides transaction management for all upstream operations.
+====
+======
[[mongo.transactions.tx-manager]]
-== Transactions with `MongoTransactionManager`
+[[mongo.transactions.reactive-tx-manager]]
+== Transactions with MongoTransactionManager & ReactiveMongoTransactionManager
-`MongoTransactionManager` is the gateway to the well known Spring transaction support. It lets applications use link:{springDocsUrl}/data-access.html#transaction[the managed transaction features of Spring].
-The `MongoTransactionManager` binds a `ClientSession` to the thread. `MongoTemplate` detects the session and operates on these resources which are associated with the transaction accordingly. `MongoTemplate` can also participate in other, ongoing transactions. The following example shows how to create and use transactions with a `MongoTransactionManager`:
+`MongoTransactionManager` / `ReactiveMongoTransactionManager` is the gateway to the well known Spring transaction support.
+It lets applications use link:{springDocsUrl}/data-access.html#transaction[the managed transaction features of Spring].
+The `MongoTransactionManager` binds a `ClientSession` to the thread whereas the `ReactiveMongoTransactionManager` is using the `ReactorContext` for this.
+`MongoTemplate` detects the session and operates on these resources which are associated with the transaction accordingly.
+`MongoTemplate` can also participate in other, ongoing transactions. The following example shows how to create and use transactions with a `MongoTransactionManager`:
-.Transactions with `MongoTransactionManager`
+.Transactions with `MongoTransactionManager` / `ReactiveMongoTransactionManager`
+[tabs]
+======
+Imperative::
++
====
-[source,java]
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
----
@Configuration
static class Config extends AbstractMongoClientConfiguration {
@@ -212,94 +296,15 @@ public class StateService {
----
<1> Register `MongoTransactionManager` in the application context.
<2> Mark methods as transactional.
-====
NOTE: `@Transactional(readOnly = true)` advises `MongoTransactionManager` to also start a transaction that adds the
- `ClientSession` to outgoing requests.
-
-[[mongo.transactions.reactive]]
-== Reactive Transactions
-
-Same as with the reactive `ClientSession` support, the `ReactiveMongoTemplate` offers dedicated methods for operating
-within a transaction without having to worry about the committing or stopping actions depending on the operations outcome.
-
-NOTE: Unless you specify a `ReactiveMongoTransactionManager` within your application context, transaction support is *DISABLED*. You can use `setSessionSynchronization(ALWAYS)` to participate in ongoing non-native MongoDB transactions.
-
-Using the plain MongoDB reactive driver API a `delete` within a transactional flow may look like this.
-
-.Native driver support
-====
-[source,java]
-----
-Mono result = Mono
- .from(client.startSession()) <1>
-
- .flatMap(session -> {
- session.startTransaction(); <2>
-
- return Mono.from(collection.deleteMany(session, ...)) <3>
-
- .onErrorResume(e -> Mono.from(session.abortTransaction()).then(Mono.error(e))) <4>
-
- .flatMap(val -> Mono.from(session.commitTransaction()).then(Mono.just(val))) <5>
-
- .doFinally(signal -> session.close()); <6>
- });
-----
-<1> First we obviously need to initiate the session.
-<2> Once we have the `ClientSession` at hand, start the transaction.
-<3> Operate within the transaction by passing on the `ClientSession` to the operation.
-<4> If the operations completes exceptionally, we need to stop the transaction and preserve the error.
-<5> Or of course, commit the changes in case of success. Still preserving the operations result.
-<6> Lastly, we need to make sure to close the session.
+`ClientSession` to outgoing requests.
====
-The culprit of the above operation is in keeping the main flows `DeleteResult` instead of the transaction outcome
-published via either `commitTransaction()` or `abortTransaction()`, which leads to a rather complicated setup.
-
-[[mongo.transactions.reactive-operator]]
-== Transactions with `TransactionalOperator`
-
-Spring Data MongoDB transactions support a `TransactionalOperator`. The following example shows how to create and use a `TransactionalOperator`:
-
-.Transactions with `TransactionalOperator`
+Reactive::
++
====
-[source,java]
-----
-template.setSessionSynchronization(ALWAYS); <1>
-
-// ...
-
-TransactionalOperator rxtx = TransactionalOperator.create(anyTxManager,
- new DefaultTransactionDefinition()); <2>
-
-
-Step step = // ...;
-template.insert(step);
-
-Mono process(step)
- .then(template.update(Step.class).apply(Update.set("state", …))
- .as(rxtx::transactional) <3>
- .then();
-----
-<1> Enable transaction synchronization for Transactional participation.
-<2> Create the `TransactionalOperator` using the provided `ReactiveTransactionManager`.
-<3> `TransactionalOperator.transactional(…)` provides transaction management for all upstream operations.
-====
-
-[[mongo.transactions.reactive-tx-manager]]
-== Transactions with `ReactiveMongoTransactionManager`
-
-`ReactiveMongoTransactionManager` is the gateway to the well known Spring transaction support.
-It allows applications to leverage https://docs.spring.io/spring-framework/docs/{springVersion}/reference/html/data-access.html#transaction[the managed transaction features of Spring].
-The `ReactiveMongoTransactionManager` binds a `ClientSession` to the subscriber `Context`.
-`ReactiveMongoTemplate` detects the session and operates on these resources which are associated with the transaction accordingly.
-`ReactiveMongoTemplate` can also participate in other, ongoing transactions.
-The following example shows how to create and use transactions with a `ReactiveMongoTransactionManager`:
-
-.Transactions with `ReactiveMongoTransactionManager`
-====
-[source,java]
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
----
@Configuration
public class Config extends AbstractReactiveMongoConfiguration {
@@ -327,9 +332,10 @@ public class StateService {
----
<1> Register `ReactiveMongoTransactionManager` in the application context.
<2> Mark methods as transactional.
-====
NOTE: `@Transactional(readOnly = true)` advises `ReactiveMongoTransactionManager` to also start a transaction that adds the `ClientSession` to outgoing requests.
+====
+======
[[mongo.transactions.behavior]]
== Special behavior inside transactions
diff --git a/src/main/antora/modules/ROOT/pages/mongodb/collation.adoc b/src/main/antora/modules/ROOT/pages/mongodb/collation.adoc
new file mode 100644
index 000000000..0f99995b9
--- /dev/null
+++ b/src/main/antora/modules/ROOT/pages/mongodb/collation.adoc
@@ -0,0 +1,90 @@
+[[mongo.collation]]
+= Collations
+
+Since version 3.4, MongoDB supports collations for collection and index creation and various query operations.
+Collations define string comparison rules based on the http://userguide.icu-project.org/collation/concepts[ICU collations].
+A collation document consists of various properties that are encapsulated in `Collation`, as the following listing shows:
+
+====
+[source,java]
+----
+Collation collation = Collation.of("fr") <1>
+
+ .strength(ComparisonLevel.secondary() <2>
+ .includeCase())
+
+ .numericOrderingEnabled() <3>
+
+ .alternate(Alternate.shifted().punct()) <4>
+
+ .forwardDiacriticSort() <5>
+
+ .normalizationEnabled(); <6>
+----
+<1> `Collation` requires a locale for creation. This can be either a string representation of the locale, a `Locale` (considering language, country, and variant) or a `CollationLocale`. The locale is mandatory for creation.
+<2> Collation strength defines comparison levels that denote differences between characters. You can configure various options (case-sensitivity, case-ordering, and others), depending on the selected strength.
+<3> Specify whether to compare numeric strings as numbers or as strings.
+<4> Specify whether the collation should consider whitespace and punctuation as base characters for purposes of comparison.
+<5> Specify whether strings with diacritics sort from back of the string, such as with some French dictionary ordering.
+<6> Specify whether to check whether text requires normalization and whether to perform normalization.
+====
+
+Collations can be used to create collections and indexes. If you create a collection that specifies a collation, the
+collation is applied to index creation and queries unless you specify a different collation. A collation is valid for a
+whole operation and cannot be specified on a per-field basis.
+
+Like other metadata, collations can be be derived from the domain type via the `collation` attribute of the `@Document`
+annotation and will be applied directly when running queries, creating collections or indexes.
+
+NOTE: Annotated collations will not be used when a collection is auto created by MongoDB on first interaction. This would
+require additional store interaction delaying the entire process. Please use `MongoOperations.createCollection` for those cases.
+
+[source,java]
+----
+Collation french = Collation.of("fr");
+Collation german = Collation.of("de");
+
+template.createCollection(Person.class, CollectionOptions.just(collation));
+
+template.indexOps(Person.class).ensureIndex(new Index("name", Direction.ASC).collation(german));
+----
+
+NOTE: MongoDB uses simple binary comparison if no collation is specified (`Collation.simple()`).
+
+Using collations with collection operations is a matter of specifying a `Collation` instance in your query or operation options, as the following two examples show:
+
+.Using collation with `find`
+====
+[source,java]
+----
+Collation collation = Collation.of("de");
+
+Query query = new Query(Criteria.where("firstName").is("Amél")).collation(collation);
+
+List results = template.find(query, Person.class);
+----
+====
+
+.Using collation with `aggregate`
+====
+[source,java]
+----
+Collation collation = Collation.of("de");
+
+AggregationOptions options = AggregationOptions.builder().collation(collation).build();
+
+Aggregation aggregation = newAggregation(
+ project("tags"),
+ unwind("tags"),
+ group("tags")
+ .count().as("count")
+).withOptions(options);
+
+AggregationResults results = template.aggregate(aggregation, "tags", TagCount.class);
+----
+====
+
+WARNING: Indexes are only used if the collation used for the operation matches the index collation.
+
+xref:mongodb/repositories/repositories.adoc[MongoDB Repositories] support `Collations` via the `collation` attribute of the `@Query` annotation.
+
diff --git a/src/main/antora/modules/ROOT/pages/mongodb/configuration.adoc b/src/main/antora/modules/ROOT/pages/mongodb/configuration.adoc
new file mode 100644
index 000000000..1034acdd7
--- /dev/null
+++ b/src/main/antora/modules/ROOT/pages/mongodb/configuration.adoc
@@ -0,0 +1,336 @@
+[[mongodb-connectors]]
+= Connecting to MongoDB
+
+One of the first tasks when using MongoDB and Spring is to create a `MongoClient` object using the IoC container.
+There are two main ways to do this, either by using Java-based bean metadata or by using XML-based bean metadata.
+
+NOTE: For those not familiar with how to configure the Spring container using Java-based bean metadata instead of XML-based metadata, see the high-level introduction in the reference docs https://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/new-in-3.0.html#new-java-configuration[here] as well as the detailed documentation https://docs.spring.io/spring-framework/docs/{springVersion}/reference/html/core.html#beans-java-instantiating-container[here].
+
+[[mongo.mongo-java-config]]
+== Registering a Mongo Instance
+
+The following example shows an example to register an instance of a `MongoClient`:
+
+.Registering `MongoClient`
+[tabs]
+======
+Imperative::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
+----
+@Configuration
+public class AppConfig {
+
+ /*
+ * Use the standard Mongo driver API to create a com.mongodb.client.MongoClient instance.
+ */
+ public @Bean com.mongodb.client.MongoClient mongoClient() {
+ return com.mongodb.client.MongoClients.create("mongodb://localhost:27017");
+ }
+}
+----
+
+Reactive::
++
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+@Configuration
+public class AppConfig {
+
+ /*
+ * Use the standard Mongo driver API to create a com.mongodb.client.MongoClient instance.
+ */
+ public @Bean com.mongodb.reactivestreams.client.MongoClient mongoClient() {
+ return com.mongodb.reactivestreams.client.MongoClients.create("mongodb://localhost:27017");
+ }
+}
+----
+
+XML::
++
+[source,xml,indent=0,subs="verbatim,quotes",role="third"]
+----
+
+
+
+
+
+
+
+----
+======
+
+This approach lets you use the standard `MongoClient` instance, with the container using Spring's `MongoClientFactoryBean`/`ReactiveMongoClientFactoryBean`.
+As compared to instantiating a `MongoClient` instance directly, the `FactoryBean` has the added advantage of also providing the container with an `ExceptionTranslator` implementation that translates MongoDB exceptions to exceptions in Spring's portable `DataAccessException` hierarchy for data access classes annotated with the `@Repository` annotation.
+This hierarchy and the use of `@Repository` is described in link:{springDocsUrl}/data-access.html[Spring's DAO support features].
+
+The following example shows an example of a Java-based bean metadata that supports exception translation on `@Repository` annotated classes:
+
+.Registering a `MongoClient` via `MongoClientFactoryBean` / `ReactiveMongoClientFactoryBean`
+[tabs]
+======
+Imperative::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
+----
+@Configuration
+public class AppConfig {
+
+ /*
+ * Factory bean that creates the com.mongodb.client.MongoClient instance
+ */
+ public @Bean MongoClientFactoryBean mongo() {
+ MongoClientFactoryBean mongo = new MongoClientFactoryBean();
+ mongo.setHost("localhost");
+ return mongo;
+ }
+}
+----
+
+Reactive::
++
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+@Configuration
+public class AppConfig {
+
+ /*
+ * Factory bean that creates the com.mongodb.reactivestreams.client.MongoClient instance
+ */
+ public @Bean ReactiveMongoClientFactoryBean mongo() {
+ ReactiveMongoClientFactoryBean mongo = new ReactiveMongoClientFactoryBean();
+ mongo.setHost("localhost");
+ return mongo;
+ }
+}
+----
+======
+
+To access the `MongoClient` object created by the `FactoryBean` in other `@Configuration` classes or your own classes, use a `private @Autowired MongoClient mongoClient;` field.
+
+[[mongo.mongo-db-factory]]
+== The MongoDatabaseFactory Interface
+
+While `MongoClient` is the entry point to the MongoDB driver API, connecting to a specific MongoDB database instance requires additional information, such as the database name and an optional username and password.
+With that information, you can obtain a `MongoDatabase` object and access all the functionality of a specific MongoDB database instance.
+Spring provides the `org.springframework.data.mongodb.core.MongoDatabaseFactory` & `org.springframework.data.mongodb.core.ReactiveMongoDatabaseFactory` interfaces, shown in the following listing, to bootstrap connectivity to the database:
+
+[tabs]
+======
+Imperative::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
+----
+public interface MongoDatabaseFactory {
+
+ MongoDatabase getDatabase() throws DataAccessException;
+
+ MongoDatabase getDatabase(String dbName) throws DataAccessException;
+}
+----
+
+Reactive::
++
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+public interface ReactiveMongoDatabaseFactory {
+
+ Mono getDatabase() throws DataAccessException;
+
+ Mono getDatabase(String dbName) throws DataAccessException;
+}
+----
+======
+
+The following sections show how you can use the container with either Java-based or XML-based metadata to configure an instance of the `MongoDatabaseFactory` interface.
+In turn, you can use the `MongoDatabaseFactory` / `ReactiveMongoDatabaseFactory` instance to configure `MongoTemplate` / `ReactiveMongoTemplate`.
+
+Instead of using the IoC container to create an instance of the template, you can use them in standard Java code, as follows:
+
+[tabs]
+======
+Imperative::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
+----
+public class MongoApplication {
+
+ public static void main(String[] args) throws Exception {
+
+ MongoOperations mongoOps = new MongoTemplate(new SimpleMongoClientDatabaseFactory(MongoClients.create(), "database"));
+
+ // ...
+ }
+}
+----
+The code in bold highlights the use of `SimpleMongoClientDbFactory` and is the only difference between the listing shown in the xref:mongodb/getting-started.adoc[getting started section].
+Use `SimpleMongoClientDbFactory` when choosing `com.mongodb.client.MongoClient` as the entrypoint of choice.
+
+Reactive::
++
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+public class ReactiveMongoApplication {
+
+ public static void main(String[] args) throws Exception {
+
+ ReactiveMongoOperations mongoOps = new MongoTemplate(new SimpleReactiveMongoDatabaseFactory(MongoClients.create(), "database"));
+
+ // ...
+ }
+}
+----
+======
+
+[[mongo.mongo-db-factory-java]]
+[[mongo.mongo-db-factory.config]]
+== Registering a `MongoDatabaseFactory` / `ReactiveMongoDatabaseFactory`
+
+To register a `MongoDatabaseFactory`/ `ReactiveMongoDatabaseFactory` instance with the container, you write code much like what was highlighted in the previous section.
+The following listing shows a simple example:
+
+[tabs]
+======
+Imperative::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
+----
+@Configuration
+public class MongoConfiguration {
+
+ @Bean
+ public MongoDatabaseFactory mongoDatabaseFactory() {
+ return new SimpleMongoClientDatabaseFactory(MongoClients.create(), "database");
+ }
+}
+----
+
+Reactive::
++
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+@Configuration
+public class ReactiveMongoConfiguration {
+
+ @Bean
+ public ReactiveMongoDatabaseFactory mongoDatabaseFactory() {
+ return new SimpleReactiveMongoDatabaseFactory(MongoClients.create(), "database");
+ }
+}
+----
+======
+
+MongoDB Server generation 3 changed the authentication model when connecting to the DB.
+Therefore, some of the configuration options available for authentication are no longer valid.
+You should use the `MongoClient`-specific options for setting credentials through `MongoCredential` to provide authentication data, as shown in the following example:
+
+[tabs]
+======
+Java::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
+----
+@Configuration
+public class MongoAppConfig extends AbstractMongoClientConfiguration {
+
+ @Override
+ public String getDatabaseName() {
+ return "database";
+ }
+
+ @Override
+ protected void configureClientSettings(Builder builder) {
+
+ builder
+ .credential(MongoCredential.createCredential("name", "db", "pwd".toCharArray()))
+ .applyToClusterSettings(settings -> {
+ settings.hosts(singletonList(new ServerAddress("127.0.0.1", 27017)));
+ });
+ }
+}
+----
+
+XML::
++
+[source,xml,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+
+----
+Username and password credentials used in XML-based configuration must be URL-encoded when these contain reserved characters, such as `:`, `%`, `@`, or `,`.
+The following example shows encoded credentials:
+`m0ng0@dmin:mo_res:bw6},Qsdxx@admin@database` -> `m0ng0%40dmin:mo_res%3Abw6%7D%2CQsdxx%40admin@database`
+See https://tools.ietf.org/html/rfc3986#section-2.2[section 2.2 of RFC 3986] for further details.
+======
+
+If you need to configure additional options on the `com.mongodb.client.MongoClient` instance that is used to create a `SimpleMongoClientDbFactory`, you can refer to an existing bean as shown in the following example. To show another common usage pattern, the following listing shows the use of a property placeholder, which lets you parametrize the configuration and the creation of a `MongoTemplate`:
+
+[tabs]
+======
+Java::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
+----
+@Configuration
+@PropertySource("classpath:/com/myapp/mongodb/config/mongo.properties")
+public class MongoAppConfig extends AbstractMongoClientConfiguration {
+
+ @Autowired
+ Environment env;
+
+ @Override
+ public String getDatabaseName() {
+ return "database";
+ }
+
+ @Override
+ protected void configureClientSettings(Builder builder) {
+
+ builder.applyToClusterSettings(settings -> {
+ settings.hosts(singletonList(
+ new ServerAddress(env.getProperty("mongo.host"), env.getProperty("mongo.port", Integer.class))));
+ });
+
+ builder.applyToConnectionPoolSettings(settings -> {
+
+ settings.maxConnectionLifeTime(env.getProperty("mongo.pool-max-life-time", Integer.class), TimeUnit.MILLISECONDS)
+ .minSize(env.getProperty("mongo.pool-min-size", Integer.class))
+ .maxSize(env.getProperty("mongo.pool-max-size", Integer.class))
+ .maintenanceFrequency(10, TimeUnit.MILLISECONDS)
+ .maintenanceInitialDelay(11, TimeUnit.MILLISECONDS)
+ .maxConnectionIdleTime(30, TimeUnit.SECONDS)
+ .maxWaitTime(15, TimeUnit.MILLISECONDS);
+ });
+ }
+}
+----
+
+XML::
++
+[source,xml,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+
+
+
+
+
+
+
+
+
+
+
+----
+======
diff --git a/src/main/antora/modules/ROOT/pages/mongodb/converters-type-mapping.adoc b/src/main/antora/modules/ROOT/pages/mongodb/converters-type-mapping.adoc
new file mode 100644
index 000000000..ea0876aa5
--- /dev/null
+++ b/src/main/antora/modules/ROOT/pages/mongodb/converters-type-mapping.adoc
@@ -0,0 +1,125 @@
+[[mongo-template.type-mapping]]
+== Type Mapping
+
+MongoDB collections can contain documents that represent instances of a variety of types.
+This feature can be useful if you store a hierarchy of classes or have a class with a property of type `Object`.In the latter case, the values held inside that property have to be read in correctly when retrieving the object.Thus, we need a mechanism to store type information alongside the actual document.
+
+To achieve that, the `MappingMongoConverter` uses a `MongoTypeMapper` abstraction with `DefaultMongoTypeMapper` as its main implementation.Its default behavior to store the fully qualified classname under `_class` inside the document.Type hints are written for top-level documents as well as for every value (if it is a complex type and a subtype of the declared property type).The following example (with a JSON representation at the end) shows how the mapping works:
+
+.Type mapping
+====
+[source,java]
+----
+class Sample {
+ Contact value;
+}
+
+abstract class Contact { … }
+
+class Person extends Contact { … }
+
+Sample sample = new Sample();
+sample.value = new Person();
+
+mongoTemplate.save(sample);
+
+{
+ "value" : { "_class" : "com.acme.Person" },
+ "_class" : "com.acme.Sample"
+}
+----
+====
+
+Spring Data MongoDB stores the type information as the last field for the actual root class as well as for the nested type (because it is complex and a subtype of `Contact`).So, if you now use `mongoTemplate.findAll(Object.class, "sample")`, you can find out that the document stored is a `Sample` instance.You can also find out that the value property is actually a `Person`.
+
+[[customizing-type-mapping]]
+=== Customizing Type Mapping
+
+If you want to avoid writing the entire Java class name as type information but would rather like to use a key, you can use the `@TypeAlias` annotation on the entity class.If you need to customize the mapping even more, have a look at the `TypeInformationMapper` interface.An instance of that interface can be configured at the `DefaultMongoTypeMapper`, which can, in turn, be configured on `MappingMongoConverter`.The following example shows how to define a type alias for an entity:
+
+.Defining a type alias for an Entity
+====
+[source,java]
+----
+@TypeAlias("pers")
+class Person {
+
+}
+----
+====
+
+Note that the resulting document contains `pers` as the value in the `_class` Field.
+
+[WARNING]
+====
+Type aliases only work if the mapping context is aware of the actual type.
+The required entity metadata is determined either on first save or has to be provided via the configurations initial entity set.
+By default, the configuration class scans the base package for potential candidates.
+
+[source,java]
+----
+@Configuration
+class AppConfig extends AbstractMongoClientConfiguration {
+
+ @Override
+ protected Set> getInitialEntitySet() {
+ return Collections.singleton(Person.class);
+ }
+
+ // ...
+}
+----
+====
+
+[[configuring-custom-type-mapping]]
+=== Configuring Custom Type Mapping
+
+The following example shows how to configure a custom `MongoTypeMapper` in `MappingMongoConverter`:
+
+[source,java]
+----
+class CustomMongoTypeMapper extends DefaultMongoTypeMapper {
+ //implement custom type mapping here
+}
+----
+
+.Configuring a custom `MongoTypeMapper`
+====
+.Java
+[source,java,role="primary"]
+----
+@Configuration
+class SampleMongoConfiguration extends AbstractMongoClientConfiguration {
+
+ @Override
+ protected String getDatabaseName() {
+ return "database";
+ }
+
+ @Bean
+ @Override
+ public MappingMongoConverter mappingMongoConverter(MongoDatabaseFactory databaseFactory,
+ MongoCustomConversions customConversions, MongoMappingContext mappingContext) {
+ MappingMongoConverter mmc = super.mappingMongoConverter();
+ mmc.setTypeMapper(customTypeMapper());
+ return mmc;
+ }
+
+ @Bean
+ public MongoTypeMapper customTypeMapper() {
+ return new CustomMongoTypeMapper();
+ }
+}
+----
+
+.XML
+[source,xml,role="secondary"]
+----
+
+
+
+----
+====
+
+Note that the preceding example extends the `AbstractMongoClientConfiguration` class and overrides the bean definition of the `MappingMongoConverter` where we configured our custom `MongoTypeMapper`.
+
diff --git a/src/main/antora/modules/ROOT/pages/mongodb/geo-json.adoc b/src/main/antora/modules/ROOT/pages/mongodb/geo-json.adoc
new file mode 100644
index 000000000..a85060459
--- /dev/null
+++ b/src/main/antora/modules/ROOT/pages/mongodb/geo-json.adoc
@@ -0,0 +1,40 @@
+TODO: add the following section somewhere
+
+[[mongo.geo-json.jackson-modules]]
+== GeoJSON Jackson Modules
+
+By using the <>, Spring Data registers additional Jackson ``Modules``s to the `ObjectMapper` for de-/serializing common Spring Data domain types.
+Please refer to the <> section to learn more about the infrastructure setup of this feature.
+
+The MongoDB module additionally registers ``JsonDeserializer``s for the following GeoJSON types via its `GeoJsonConfiguration` exposing the `GeoJsonModule`.
+----
+org.springframework.data.mongodb.core.geo.GeoJsonPoint
+org.springframework.data.mongodb.core.geo.GeoJsonMultiPoint
+org.springframework.data.mongodb.core.geo.GeoJsonLineString
+org.springframework.data.mongodb.core.geo.GeoJsonMultiLineString
+org.springframework.data.mongodb.core.geo.GeoJsonPolygon
+org.springframework.data.mongodb.core.geo.GeoJsonMultiPolygon
+----
+
+[NOTE]
+====
+The `GeoJsonModule` only registers ``JsonDeserializer``s! +
+To equip the `ObjectMapper` with a symmetric set of ``JsonSerializer``s you need to either manually configure those for the `ObjectMapper` or provide a custom `SpringDataJacksonModules` configuration exposing `GeoJsonModule.serializers()` as a Spring Bean.
+
+[source,java]
+----
+class GeoJsonConfiguration implements SpringDataJacksonModules {
+
+ @Bean
+ public Module geoJsonSerializers() {
+ return GeoJsonModule.serializers();
+ }
+}
+----
+====
+
+[WARNING]
+====
+The next major version (`4.0`) will register both, ``JsonDeserializer``s and ``JsonSerializer``s for GeoJSON types by default.
+====
+
diff --git a/src/main/antora/modules/ROOT/pages/mongodb/getting-started.adoc b/src/main/antora/modules/ROOT/pages/mongodb/getting-started.adoc
new file mode 100644
index 000000000..e12fdc2a4
--- /dev/null
+++ b/src/main/antora/modules/ROOT/pages/mongodb/getting-started.adoc
@@ -0,0 +1,62 @@
+[[mongodb-getting-started]]
+= Getting Started
+
+An easy way to bootstrap setting up a working environment is to create a Spring-based project via https://start.spring.io/#!type=maven-project&dependencies=data-mongodb[start.spring.io] or create a Spring project in https://spring.io/tools[Spring Tools].
+
+[[mongo.examples-repo]]
+== Examples Repository
+
+The GitHub https://github.com/spring-projects/spring-data-examples[spring-data-examples repository] hosts several examples that you can download and play around with to get a feel for how the library works.
+
+[[mongodb.hello-world]]
+== Hello World
+
+First, you need to set up a running MongoDB server. Refer to the https://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: `/bin/mongod`
+
+Then you can create a `Person` class to persist:
+
+====
+[source,java]
+----
+include::example$Person.java[tags=file]
+----
+====
+
+You also need a main application to run:
+
+[tabs]
+======
+Imperative::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
+----
+include::example$MongoApplication.java[tags=file]
+----
+
+Reactive::
++
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+include::example$ReactiveMongoApplication.java[tags=file]
+----
+======
+
+When you run the main program, the preceding examples produce the following output:
+
+[source]
+----
+10:01:32,265 DEBUG o.s.data.mongodb.core.MongoTemplate - insert Document containing fields: [_class, age, name] in collection: Person
+10:01:32,765 DEBUG o.s.data.mongodb.core.MongoTemplate - findOne using query: { "name" : "Joe"} in db.collection: database.Person
+Person [id=4ddbba3c0be56b7e1b210166, name=Joe, age=34]
+10:01:32,984 DEBUG o.s.data.mongodb.core.MongoTemplate - Dropped collection [database.person]
+----
+
+Even in this simple example, there are few things to notice:
+
+* You can instantiate the central helper class of Spring Mongo, xref:mongodb/template-api.adoc[`MongoTemplate`], by using the standard or reactive `MongoClient` 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 xref:mongodb/mapping/mapping.adoc[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
+
diff --git a/src/main/asciidoc/reference/jmx.adoc b/src/main/antora/modules/ROOT/pages/mongodb/jmx.adoc
similarity index 100%
rename from src/main/asciidoc/reference/jmx.adoc
rename to src/main/antora/modules/ROOT/pages/mongodb/jmx.adoc
diff --git a/src/main/asciidoc/reference/mongo-custom-conversions.adoc b/src/main/antora/modules/ROOT/pages/mongodb/mapping/custom-conversions.adoc
similarity index 84%
rename from src/main/asciidoc/reference/mongo-custom-conversions.adoc
rename to src/main/antora/modules/ROOT/pages/mongodb/mapping/custom-conversions.adoc
index 8c05e4225..7fcc0fa14 100644
--- a/src/main/asciidoc/reference/mongo-custom-conversions.adoc
+++ b/src/main/antora/modules/ROOT/pages/mongodb/mapping/custom-conversions.adoc
@@ -1,9 +1,10 @@
-[[mongo.custom-converters]]
-== Custom Conversions - Overriding Default Mapping
+include::{commons}@data-commons::page$custom-conversions.adoc[]
-The most trivial way of influencing the mapping result is by specifying the desired native MongoDB target type via the
-`@Field` annotation. This allows to work with non MongoDB types like `BigDecimal` in the domain model while persisting
-values in native `org.bson.types.Decimal128` format.
+[[mongo.custom-converters]]
+== Type based Converter
+
+The most trivial way of influencing the mapping result is by specifying the desired native MongoDB target type via the `@Field` annotation.
+This allows to work with non MongoDB types like `BigDecimal` in the domain model while persisting values in native `org.bson.types.Decimal128` format.
.Explicit target type mapping
====
@@ -20,6 +21,7 @@ public class Payment {
}
----
+
[source,java]
----
{
@@ -28,7 +30,7 @@ public class Payment {
"date" : ISODate("2019-04-03T12:11:01.870Z") <3>
}
----
-<1> String _id_ values that represent a valid `ObjectId` are converted automatically. See <>
+<1> String _id_ values that represent a valid `ObjectId` are converted automatically. See xref:mongodb/template-id-handling.adoc#mongo-template.id-handling[How the `_id` Field is Handled in the Mapping Layer]
for details.
<2> The desired target type is explicitly defined as `Decimal128` which translates to `NumberDecimal`. Otherwise the
`BigDecimal` value would have been truned into a `String`.
@@ -43,7 +45,7 @@ The `MappingMongoConverter` checks to see if any Spring converters can handle a
NOTE: For more information on the Spring type conversion service, see the reference docs link:{springDocsUrl}/core.html#validation[here].
[[mongo.custom-converters.writer]]
-=== Saving by Using a Registered Spring Converter
+=== Writing Converter
The following example shows an implementation of the `Converter` that converts from a `Person` object to a `org.bson.Document`:
@@ -66,7 +68,7 @@ public class PersonWriteConverter implements Converter {
----
[[mongo.custom-converters.reader]]
-=== Reading by Using a Spring Converter
+=== Reading Converter
The following example shows an implementation of a `Converter` that converts from a `Document` to a `Person` object:
@@ -83,7 +85,7 @@ public class PersonReadConverter implements Converter {
----
[[mongo.custom-converters.xml]]
-=== Registering Spring Converters with the `MongoConverter`
+=== Registering Converters
[source,java]
----
@@ -101,5 +103,3 @@ class MyMongoConfiguration extends AbstractMongoClientConfiguration {
}
}
----
-
-include::../{spring-data-commons-docs}/custom-conversions.adoc[leveloffset=+3]
diff --git a/src/main/asciidoc/reference/document-references.adoc b/src/main/antora/modules/ROOT/pages/mongodb/mapping/document-references.adoc
similarity index 98%
rename from src/main/asciidoc/reference/document-references.adoc
rename to src/main/antora/modules/ROOT/pages/mongodb/mapping/document-references.adoc
index a3ddd1cba..d1ef6b806 100644
--- a/src/main/asciidoc/reference/document-references.adoc
+++ b/src/main/antora/modules/ROOT/pages/mongodb/mapping/document-references.adoc
@@ -1,5 +1,5 @@
[[mapping-usage-references]]
-=== Using DBRefs
+= Using DBRefs
The mapping framework does not have to store child objects embedded within the document.
You can also store them separately and use a `DBRef` to refer to that document.
@@ -53,10 +53,10 @@ CAUTION: Lazy loading may require class proxies, that in turn, might need access
For those cases please consider falling back to an interface type (eg. switch from `ArrayList` to `List`) or provide the required `--add-opens` argument.
[[mapping-usage.document-references]]
-=== Using Document References
+== Using Document References
Using `@DocumentReference` offers a flexible way of referencing entities in MongoDB.
-While the goal is the same as when using <>, the store representation is different.
+While the goal is the same as when using xref:mongodb/mapping/document-references.adoc[DBRefs], the store representation is different.
`DBRef` resolves to a document with a fixed structure as outlined in the https://docs.mongodb.com/manual/reference/database-references/[MongoDB Reference documentation]. +
Document references, do not follow a specific format.
They can be literally anything, a single value, an entire document, basically everything that can be stored in MongoDB.
diff --git a/src/main/asciidoc/reference/mongo-entity-callbacks.adoc b/src/main/antora/modules/ROOT/pages/mongodb/mapping/entity-callbacks.adoc
similarity index 93%
rename from src/main/asciidoc/reference/mongo-entity-callbacks.adoc
rename to src/main/antora/modules/ROOT/pages/mongodb/mapping/entity-callbacks.adoc
index 3bb2a34ea..3ae3cb86f 100644
--- a/src/main/asciidoc/reference/mongo-entity-callbacks.adoc
+++ b/src/main/antora/modules/ROOT/pages/mongodb/mapping/entity-callbacks.adoc
@@ -1,5 +1,7 @@
+include::{commons}@data-commons::page$entity-callbacks.adoc[]
+
[[mongo.entity-callbacks]]
-= Store specific EntityCallbacks
+== Store specific EntityCallbacks
Spring Data MongoDB uses the `EntityCallback` API for its auditing support and reacts on the following callbacks.
diff --git a/src/main/antora/modules/ROOT/pages/mongodb/mapping/lifecycle-events.adoc b/src/main/antora/modules/ROOT/pages/mongodb/mapping/lifecycle-events.adoc
new file mode 100644
index 000000000..8050b50c6
--- /dev/null
+++ b/src/main/antora/modules/ROOT/pages/mongodb/mapping/lifecycle-events.adoc
@@ -0,0 +1,56 @@
+[[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 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.
+
+Entity lifecycle events can be costly and you may notice a change in the performance profile when loading large result sets.
+You can disable lifecycle events on the link:https://docs.spring.io/spring-data/mongodb/docs/{version}/api/org/springframework/data/mongodb/core/MongoTemplate.html#setEntityLifecycleEventsEnabled(boolean)[Template API].
+
+To intercept an object before it goes through the conversion process (which turns your domain object into a `org.bson.Document`), 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:
+
+====
+[source,java]
+----
+public class BeforeConvertListener extends AbstractMongoEventListener {
+ @Override
+ public void onBeforeConvert(BeforeConvertEvent event) {
+ ... does some auditing manipulation, set timestamps, whatever ...
+ }
+}
+----
+====
+
+To intercept an object before it goes into the database, you can register a subclass of `org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener` that overrides the `onBeforeSave` method. When the event is dispatched, your listener is called and passed the domain object and the converted `com.mongodb.Document`. The following example shows how to do so:
+
+====
+[source,java]
+----
+public class BeforeSaveListener extends AbstractMongoEventListener {
+ @Override
+ public void onBeforeSave(BeforeSaveEvent event) {
+ … change values, delete them, whatever …
+ }
+}
+----
+====
+
+Declaring these beans in your Spring ApplicationContext causes them to be invoked whenever the event is dispatched.
+
+.Callbacks on `AbstractMappingEventListener`:
+[%collapsible]
+====
+* `onBeforeConvert`: Called in `MongoTemplate` `insert`, `insertList`, and `save` operations before the object is converted to a `Document` by a `MongoConverter`.
+* `onBeforeSave`: Called in `MongoTemplate` `insert`, `insertList`, and `save` operations *before* inserting or saving the `Document` in the database.
+* `onAfterSave`: Called in `MongoTemplate` `insert`, `insertList`, and `save` operations *after* inserting or saving the `Document` in the database.
+* `onAfterLoad`: Called in `MongoTemplate` `find`, `findAndRemove`, `findOne`, and `getCollection` methods after the `Document` has been retrieved from the database.
+* `onAfterConvert`: Called in `MongoTemplate` `find`, `findAndRemove`, `findOne`, and `getCollection` methods after the `Document` has been retrieved from the database was converted to a POJO.
+====
+
+NOTE: Lifecycle events are only emitted for root level types. Complex types used as properties within a document root are not subject to event publication unless they are document references annotated with `@DBRef`.
+
+WARNING: Lifecycle events depend on an `ApplicationEventMulticaster`, which in case of the `SimpleApplicationEventMulticaster` can be configured with a `TaskExecutor`, and therefore gives no guarantees when an Event is processed.
+
+
diff --git a/src/main/antora/modules/ROOT/pages/mongodb/mapping/mapping-index-management.adoc b/src/main/antora/modules/ROOT/pages/mongodb/mapping/mapping-index-management.adoc
new file mode 100644
index 000000000..b094b925d
--- /dev/null
+++ b/src/main/antora/modules/ROOT/pages/mongodb/mapping/mapping-index-management.adoc
@@ -0,0 +1,340 @@
+[[mapping.index-creation]]
+= Index Creation
+
+Spring Data MongoDB can automatically create indexes for entity types annotated with `@Document`.
+Index creation must be explicitly enabled since version 3.0 to prevent undesired effects with collection lifecyle and performance impact.
+Indexes are automatically created for the initial entity set on application startup and when accessing an entity type for the first time while the application runs.
+
+We generally recommend explicit index creation for application-based control of indexes as Spring Data cannot automatically create indexes for collections that were recreated while the application was running.
+
+`IndexResolver` provides an abstraction for programmatic index definition creation if you want to make use of `@Indexed` annotations such as `@GeoSpatialIndexed`, `@TextIndexed`, `@CompoundIndex` and `@WildcardIndexed`.
+You can use index definitions with `IndexOperations` to create indexes.
+A good point in time for index creation is on application startup, specifically after the application context was refreshed, triggered by observing `ContextRefreshedEvent`.
+This event guarantees that the context is fully initialized.
+Note that at this time other components, especially bean factories might have access to the MongoDB database.
+
+[WARNING]
+====
+``Map``-like properties are skipped by the `IndexResolver` unless annotated with `@WildcardIndexed` because the _map key_ must be part of the index definition. Since the purpose of maps is the usage of dynamic keys and values, the keys cannot be resolved from static mapping metadata.
+====
+
+.Programmatic Index Creation for a single Domain Type
+====
+[source,java]
+----
+class MyListener {
+
+ @EventListener(ContextRefreshedEvent.class)
+ public void initIndicesAfterStartup() {
+
+ MappingContext extends MongoPersistentEntity>, MongoPersistentProperty> mappingContext = mongoTemplate
+ .getConverter().getMappingContext();
+
+ IndexResolver resolver = new MongoPersistentEntityIndexResolver(mappingContext);
+
+ IndexOperations indexOps = mongoTemplate.indexOps(DomainType.class);
+ resolver.resolveIndexFor(DomainType.class).forEach(indexOps::ensureIndex);
+ }
+}
+----
+====
+
+.Programmatic Index Creation for all Initial Entities
+====
+[source,java]
+----
+class MyListener{
+
+ @EventListener(ContextRefreshedEvent.class)
+ public void initIndicesAfterStartup() {
+
+ MappingContext extends MongoPersistentEntity>, MongoPersistentProperty> mappingContext = mongoTemplate
+ .getConverter().getMappingContext();
+
+ // consider only entities that are annotated with @Document
+ mappingContext.getPersistentEntities()
+ .stream()
+ .filter(it -> it.isAnnotationPresent(Document.class))
+ .forEach(it -> {
+
+ IndexOperations indexOps = mongoTemplate.indexOps(it.getType());
+ resolver.resolveIndexFor(it.getType()).forEach(indexOps::ensureIndex);
+ });
+ }
+}
+----
+====
+
+Alternatively, if you want to ensure index and collection presence before any component is able to access your database from your application, declare a `@Bean` method for `MongoTemplate` and include the code from above before returning the `MongoTemplate` object.
+
+[NOTE]
+====
+To turn automatic index creation _ON_ please override `autoIndexCreation()` in your configuration.
+[source,java]
+----
+@Configuration
+public class Config extends AbstractMongoClientConfiguration {
+
+ @Override
+ public boolean autoIndexCreation() {
+ return true;
+ }
+
+// ...
+}
+----
+====
+
+IMPORTANT: Automatic index creation is turned _OFF_ by default as of version 3.0.
+
+[[mapping-usage-indexes.compound-index]]
+== Compound Indexes
+
+Compound indexes are also supported. They are defined at the class level, rather than on individual properties.
+
+NOTE: Compound indexes are very important to improve the performance of queries that involve criteria on multiple fields
+
+Here's an example that creates a compound index of `lastName` in ascending order and `age` in descending order:
+
+.Example Compound Index Usage
+====
+[source,java]
+----
+package com.mycompany.domain;
+
+@Document
+@CompoundIndex(name = "age_idx", def = "{'lastName': 1, 'age': -1}")
+public class Person {
+
+ @Id
+ private ObjectId id;
+ private Integer age;
+ private String firstName;
+ private String lastName;
+
+}
+----
+====
+
+[TIP]
+====
+`@CompoundIndex` is repeatable using `@CompoundIndexes` as its container.
+
+[source,java]
+----
+@Document
+@CompoundIndex(name = "cmp-idx-one", def = "{'firstname': 1, 'lastname': -1}")
+@CompoundIndex(name = "cmp-idx-two", def = "{'address.city': -1, 'address.street': 1}")
+public class Person {
+
+ String firstname;
+ String lastname;
+
+ Address address;
+
+ // ...
+}
+----
+====
+
+[[mapping-usage-indexes.hashed-index]]
+== Hashed Indexes
+
+Hashed indexes allow hash based sharding within a sharded cluster.
+Using hashed field values to shard collections results in a more random distribution.
+For details, refer to the https://docs.mongodb.com/manual/core/index-hashed/[MongoDB Documentation].
+
+Here's an example that creates a hashed index for `_id`:
+
+.Example Hashed Index Usage
+====
+[source,java]
+----
+@Document
+public class DomainType {
+
+ @HashIndexed @Id String id;
+
+ // ...
+}
+----
+====
+
+Hashed indexes can be created next to other index definitions like shown below, in that case both indices are created:
+
+.Example Hashed Index Usage togehter with simple index
+====
+[source,java]
+----
+@Document
+public class DomainType {
+
+ @Indexed
+ @HashIndexed
+ String value;
+
+ // ...
+}
+----
+====
+
+In case the example above is too verbose, a compound annotation allows to reduce the number of annotations that need to be declared on a property:
+
+.Example Composed Hashed Index Usage
+====
+[source,java]
+----
+@Document
+public class DomainType {
+
+ @IndexAndHash(name = "idx...") <1>
+ String value;
+
+ // ...
+}
+
+@Indexed
+@HashIndexed
+@Retention(RetentionPolicy.RUNTIME)
+public @interface IndexAndHash {
+
+ @AliasFor(annotation = Indexed.class, attribute = "name") <1>
+ String name() default "";
+}
+----
+<1> Potentially register an alias for certain attributes of the meta annotation.
+====
+
+[NOTE]
+====
+Although index creation via annotations comes in handy for many scenarios cosider taking over more control by setting up indices manually via `IndexOperations`.
+
+[source,java]
+----
+mongoOperations.indexOpsFor(Jedi.class)
+ .ensureIndex(HashedIndex.hashed("useTheForce"));
+----
+====
+
+[[mapping-usage-indexes.wildcard-index]]
+== Wildcard Indexes
+
+A `WildcardIndex` is an index that can be used to include all fields or specific ones based a given (wildcard) pattern.
+For details, refer to the https://docs.mongodb.com/manual/core/index-wildcard/[MongoDB Documentation].
+
+The index can be set up programmatically using `WildcardIndex` via `IndexOperations`.
+
+.Programmatic WildcardIndex setup
+====
+[source,java]
+----
+mongoOperations
+ .indexOps(User.class)
+ .ensureIndex(new WildcardIndex("userMetadata"));
+----
+[source,javascript]
+----
+db.user.createIndex({ "userMetadata.$**" : 1 }, {})
+----
+====
+
+The `@WildcardIndex` annotation allows a declarative index setup that can used either with a document type or property.
+
+If placed on a type that is a root level domain entity (one annotated with `@Document`) , the index resolver will create a
+wildcard index for it.
+
+.Wildcard index on domain type
+====
+[source,java]
+----
+@Document
+@WildcardIndexed
+public class Product {
+ // …
+}
+----
+[source,javascript]
+----
+db.product.createIndex({ "$**" : 1 },{})
+----
+====
+
+The `wildcardProjection` can be used to specify keys to in-/exclude in the index.
+
+.Wildcard index with `wildcardProjection`
+====
+[source,java]
+----
+@Document
+@WildcardIndexed(wildcardProjection = "{ 'userMetadata.age' : 0 }")
+public class User {
+ private @Id String id;
+ private UserMetadata userMetadata;
+}
+----
+[source,javascript]
+----
+db.user.createIndex(
+ { "$**" : 1 },
+ { "wildcardProjection" :
+ { "userMetadata.age" : 0 }
+ }
+)
+----
+====
+
+Wildcard indexes can also be expressed by adding the annotation directly to the field.
+Please note that `wildcardProjection` is not allowed on nested paths such as properties.
+Projections on types annotated with `@WildcardIndexed` are omitted during index creation.
+
+.Wildcard index on property
+====
+[source,java]
+----
+@Document
+public class User {
+ private @Id String id;
+
+ @WildcardIndexed
+ private UserMetadata userMetadata;
+}
+----
+[source,javascript]
+----
+db.user.createIndex({ "userMetadata.$**" : 1 }, {})
+----
+====
+
+[[mapping-usage-indexes.text-index]]
+== Text Indexes
+
+NOTE: The text index feature is disabled by default for MongoDB v.2.4.
+
+Creating a text index allows accumulating several fields into a searchable full-text index.
+It is only possible to have one text index per collection, so all fields marked with `@TextIndexed` are combined into this index.
+Properties can be weighted to influence the document score for ranking results.
+The default language for the text index is English.To change the default language, set the `language` attribute to whichever language you want (for example,`@Document(language="spanish")`).
+Using a property called `language` or `@Language` lets you define a language override on a per-document base.
+The following example shows how to created a text index and set the language to Spanish:
+
+.Example Text Index Usage
+====
+[source,java]
+----
+@Document(language = "spanish")
+class SomeEntity {
+
+ @TextIndexed String foo;
+
+ @Language String lang;
+
+ Nested nested;
+}
+
+class Nested {
+
+ @TextIndexed(weight=5) String bar;
+ String roo;
+}
+----
+====
diff --git a/src/main/asciidoc/reference/mapping.adoc b/src/main/antora/modules/ROOT/pages/mongodb/mapping/mapping.adoc
similarity index 67%
rename from src/main/asciidoc/reference/mapping.adoc
rename to src/main/antora/modules/ROOT/pages/mongodb/mapping/mapping.adoc
index 2c9b243f2..4b0b7fb39 100644
--- a/src/main/asciidoc/reference/mapping.adoc
+++ b/src/main/antora/modules/ROOT/pages/mongodb/mapping/mapping.adoc
@@ -1,16 +1,14 @@
-
[[mapping-chapter]]
-= Mapping
+= Object Mapping
-Rich mapping support is provided by the `MappingMongoConverter`. `MappingMongoConverter` has a rich metadata model that provides a full feature set to map domain objects to MongoDB documents.
+Rich mapping support is provided by the `MappingMongoConverter`.
+The converter holds a metadata model that provides a full feature set to map domain objects to MongoDB documents.
The mapping metadata model is populated by using annotations on your domain objects.
However, the infrastructure is not limited to using annotations as the only source of metadata information.
The `MappingMongoConverter` also lets you map objects to documents without providing any additional metadata, by following a set of conventions.
This section describes the features of the `MappingMongoConverter`, including fundamentals, how to use conventions for mapping objects to documents and how to override those conventions with annotation-based mapping metadata.
-include::../{spring-data-commons-docs}/object-mapping.adoc[leveloffset=+1]
-
[[mapping-conventions]]
== Convention-based Mapping
@@ -68,11 +66,14 @@ When querying and updating `MongoTemplate` will use the converter to handle conv
[[mapping-conversion]]
== Data Mapping and Type Conversion
-This section explains how types are mapped to and from a MongoDB representation. Spring Data MongoDB supports all types that can be represented as BSON, MongoDB's internal document format.
-In addition to these types, Spring Data MongoDB provides a set of built-in converters to map additional types. You can provide your own converters to adjust type conversion. See <> for further details.
-
-The following provides samples of each available type conversion:
+Spring Data MongoDB supports all types that can be represented as BSON, MongoDB's internal document format.
+In addition to these types, Spring Data MongoDB provides a set of built-in converters to map additional types.
+You can provide your own converters to adjust type conversion.
+See xref:mongodb/mapping/custom-conversions.adoc[Custom Conversions - Overriding Default Mapping] for further details.
+.Built in Type conversions:
+[%collapsible]
+====
[cols="3,1,6", options="header"]
.Type
|===
@@ -193,12 +194,12 @@ calling `get()` before the actual conversion
| `LocalDate` +
(Joda, Java 8, JSR310-BackPort)
-| converter / native (Java8)footnote:[Uses UTC zone offset. Configure via <>]
+| converter / native (Java8)footnote:[Uses UTC zone offset. Configure via xref:mongodb/mapping/mapping.adoc#mapping-configuration[MongoConverterConfigurationAdapter]]
| `{"date" : ISODate("2019-11-12T00:00:00.000Z")}`
| `LocalDateTime`, `LocalTime` +
(Joda, Java 8, JSR310-BackPort)
-| converter / native (Java8)footnote:[Uses UTC zone offset. Configure via <>]
+| converter / native (Java8)footnote:[Uses UTC zone offset. Configure via xref:mongodb/mapping/mapping.adoc#mapping-configuration[MongoConverterConfigurationAdapter]]
| `{"date" : ISODate("2019-11-12T23:00:00.809Z")}`
| `DateTime` (Joda)
@@ -259,7 +260,7 @@ calling `get()` before the actual conversion
[ [ -73.97880 , 40.77247 ], [ -73.97036 , 40.76811 ] ]
] }}`
|===
-
+====
[[mapping-configuration]]
== Mapping Configuration
@@ -268,9 +269,12 @@ Unless explicitly configured, an instance of `MappingMongoConverter` is created
You can configure the `MappingMongoConverter` as well as `com.mongodb.client.MongoClient` and MongoTemplate by using either Java-based or XML-based metadata. The following example shows the configuration:
-====
-.Java
-[source,java,role="primary"]
+
+[tabs]
+======
+Java::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
----
@Configuration
public class MongoConfig extends AbstractMongoClientConfiguration {
@@ -299,11 +303,13 @@ public class MongoConfig extends AbstractMongoClientConfiguration {
return new LoggingEventListener();
}
}
-
----
+<1> The mapping base package defines the root path used to scan for entities used to pre initialize the `MappingContext`. By default the configuration classes package is used.
+<2> Configure additional custom converters for specific domain types that replace the default mapping procedure for those types with your custom implementation.
-.XML
-[source,xml,role="secondary"]
+XML::
++
+[source,xml,indent=0,subs="verbatim,quotes",role="secondary"]
----
----
-<1> The mapping base package defines the root path used to scan for entities used to pre initialize the `MappingContext`. By default the configuration classes package is used.
-<2> Configure additional custom converters for specific domain types that replace the default mapping procedure for those types with your custom implementation.
-====
+======
-`AbstractMongoClientConfiguration` requires you to implement methods that define a `com.mongodb.client.MongoClient` as well as provide a database name. `AbstractMongoClientConfiguration` also has a method named `getMappingBasePackage(…)` that you can override to tell the converter where to scan for classes annotated with the `@Document` annotation.
+`AbstractMongoClientConfiguration` requires you to implement methods that define a `com.mongodb.client.MongoClient` as well as provide a database name.
+`AbstractMongoClientConfiguration` 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 `customConversionsConfiguration` method.
MongoDB's native JSR-310 support can be enabled through `MongoConverterConfigurationAdapter.useNativeDriverJavaTimeCodecs()`.
@@ -403,96 +408,7 @@ public class Person {
IMPORTANT: The `@Id` annotation tells the mapper which property you want to use for the MongoDB `_id` property, and the `@Indexed` annotation tells the mapping framework to call `createIndex(…)` on that property of your document, making searches faster.
Automatic index creation is only done for types annotated with `@Document`.
-WARNING: Auto index creation is **disabled** by default and needs to be enabled through the configuration (see <>).
-
-[[mapping.index-creation]]
-=== Index Creation
-
-Spring Data MongoDB can automatically create indexes for entity types annotated with `@Document`.
-Index creation must be explicitly enabled since version 3.0 to prevent undesired effects with collection lifecyle and performance impact.
-Indexes are automatically created for the initial entity set on application startup and when accessing an entity type for the first time while the application runs.
-
-We generally recommend explicit index creation for application-based control of indexes as Spring Data cannot automatically create indexes for collections that were recreated while the application was running.
-
-`IndexResolver` provides an abstraction for programmatic index definition creation if you want to make use of `@Indexed` annotations such as `@GeoSpatialIndexed`, `@TextIndexed`, `@CompoundIndex` and `@WildcardIndexed`.
-You can use index definitions with `IndexOperations` to create indexes.
-A good point in time for index creation is on application startup, specifically after the application context was refreshed, triggered by observing `ContextRefreshedEvent`.
-This event guarantees that the context is fully initialized.
-Note that at this time other components, especially bean factories might have access to the MongoDB database.
-
-[WARNING]
-====
-``Map``-like properties are skipped by the `IndexResolver` unless annotated with `@WildcardIndexed` because the _map key_ must be part of the index definition. Since the purpose of maps is the usage of dynamic keys and values, the keys cannot be resolved from static mapping metadata.
-====
-
-.Programmatic Index Creation for a single Domain Type
-====
-[source,java]
-----
-class MyListener {
-
- @EventListener(ContextRefreshedEvent.class)
- public void initIndicesAfterStartup() {
-
- MappingContext extends MongoPersistentEntity>, MongoPersistentProperty> mappingContext = mongoTemplate
- .getConverter().getMappingContext();
-
- IndexResolver resolver = new MongoPersistentEntityIndexResolver(mappingContext);
-
- IndexOperations indexOps = mongoTemplate.indexOps(DomainType.class);
- resolver.resolveIndexFor(DomainType.class).forEach(indexOps::ensureIndex);
- }
-}
-----
-====
-
-.Programmatic Index Creation for all Initial Entities
-====
-[source,java]
-----
-class MyListener{
-
- @EventListener(ContextRefreshedEvent.class)
- public void initIndicesAfterStartup() {
-
- MappingContext extends MongoPersistentEntity>, MongoPersistentProperty> mappingContext = mongoTemplate
- .getConverter().getMappingContext();
-
- // consider only entities that are annotated with @Document
- mappingContext.getPersistentEntities()
- .stream()
- .filter(it -> it.isAnnotationPresent(Document.class))
- .forEach(it -> {
-
- IndexOperations indexOps = mongoTemplate.indexOps(it.getType());
- resolver.resolveIndexFor(it.getType()).forEach(indexOps::ensureIndex);
- });
- }
-}
-----
-====
-
-Alternatively, if you want to ensure index and collection presence before any component is able to access your database from your application, declare a `@Bean` method for `MongoTemplate` and include the code from above before returning the `MongoTemplate` object.
-
-[NOTE]
-====
-To turn automatic index creation _ON_ please override `autoIndexCreation()` in your configuration.
-[source,java]
-----
-@Configuration
-public class Config extends AbstractMongoClientConfiguration {
-
- @Override
- public boolean autoIndexCreation() {
- return true;
- }
-
-// ...
-}
-----
-====
-
-IMPORTANT: Automatic index creation is turned _OFF_ by default as of version 3.0.
+WARNING: Auto index creation is **disabled** by default and needs to be enabled through the configuration (see xref:mongodb/mapping/mapping.adoc#mapping.index-creation[Index Creation]).
[[mapping-usage-annotations]]
=== Mapping Annotation Overview
@@ -518,8 +434,9 @@ The MappingMongoConverter can use metadata to drive the mapping of objects to do
The mapping metadata infrastructure is defined in a separate spring-data-commons project that is technology agnostic. Specific subclasses are using in the MongoDB support to support annotation based metadata. Other strategies are also possible to put in place if there is demand.
-Here is an example of a more complex mapping.
-
+.Here is an example of a more complex mapping
+[%collapsible]
+====
[source,java]
----
@Document
@@ -574,6 +491,7 @@ public class Person {
// other getters/setters omitted
}
----
+====
[TIP]
====
@@ -648,268 +566,9 @@ NOTE: The SpEL expression in the `@Value` annotation of the `quantity` parameter
Additional examples for using the `@PersistenceConstructor` annotation can be found in the https://github.com/spring-projects/spring-data-mongodb/blob/master/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java[MappingMongoConverterUnitTests] test suite.
-[[mapping-usage-indexes.compound-index]]
-=== Compound Indexes
-
-Compound indexes are also supported. They are defined at the class level, rather than on individual properties.
-
-NOTE: Compound indexes are very important to improve the performance of queries that involve criteria on multiple fields
-
-Here's an example that creates a compound index of `lastName` in ascending order and `age` in descending order:
-
-.Example Compound Index Usage
-====
-[source,java]
-----
-package com.mycompany.domain;
-
-@Document
-@CompoundIndex(name = "age_idx", def = "{'lastName': 1, 'age': -1}")
-public class Person {
-
- @Id
- private ObjectId id;
- private Integer age;
- private String firstName;
- private String lastName;
-
-}
-----
-====
-
-[TIP]
-====
-`@CompoundIndex` is repeatable using `@CompoundIndexes` as its container.
-
-[source,java]
-----
-@Document
-@CompoundIndex(name = "cmp-idx-one", def = "{'firstname': 1, 'lastname': -1}")
-@CompoundIndex(name = "cmp-idx-two", def = "{'address.city': -1, 'address.street': 1}")
-public class Person {
-
- String firstname;
- String lastname;
-
- Address address;
-
- // ...
-}
-----
-====
-
-[[mapping-usage-indexes.hashed-index]]
-=== Hashed Indexes
-
-Hashed indexes allow hash based sharding within a sharded cluster.
-Using hashed field values to shard collections results in a more random distribution.
-For details, refer to the https://docs.mongodb.com/manual/core/index-hashed/[MongoDB Documentation].
-
-Here's an example that creates a hashed index for `_id`:
-
-.Example Hashed Index Usage
-====
-[source,java]
-----
-@Document
-public class DomainType {
-
- @HashIndexed @Id String id;
-
- // ...
-}
-----
-====
-
-Hashed indexes can be created next to other index definitions like shown below, in that case both indices are created:
-
-.Example Hashed Index Usage togehter with simple index
-====
-[source,java]
-----
-@Document
-public class DomainType {
-
- @Indexed
- @HashIndexed
- String value;
-
- // ...
-}
-----
-====
-
-In case the example above is too verbose, a compound annotation allows to reduce the number of annotations that need to be declared on a property:
-
-.Example Composed Hashed Index Usage
-====
-[source,java]
-----
-@Document
-public class DomainType {
-
- @IndexAndHash(name = "idx...") <1>
- String value;
-
- // ...
-}
-
-@Indexed
-@HashIndexed
-@Retention(RetentionPolicy.RUNTIME)
-public @interface IndexAndHash {
-
- @AliasFor(annotation = Indexed.class, attribute = "name") <1>
- String name() default "";
-}
-----
-<1> Potentially register an alias for certain attributes of the meta annotation.
-====
-
-[NOTE]
-====
-Although index creation via annotations comes in handy for many scenarios cosider taking over more control by setting up indices manually via `IndexOperations`.
-
-[source,java]
-----
-mongoOperations.indexOpsFor(Jedi.class)
- .ensureIndex(HashedIndex.hashed("useTheForce"));
-----
-====
-
-[[mapping-usage-indexes.wildcard-index]]
-=== Wildcard Indexes
-
-A `WildcardIndex` is an index that can be used to include all fields or specific ones based a given (wildcard) pattern.
-For details, refer to the https://docs.mongodb.com/manual/core/index-wildcard/[MongoDB Documentation].
-
-The index can be set up programmatically using `WildcardIndex` via `IndexOperations`.
-
-.Programmatic WildcardIndex setup
-====
-[source,java]
-----
-mongoOperations
- .indexOps(User.class)
- .ensureIndex(new WildcardIndex("userMetadata"));
-----
-[source,javascript]
-----
-db.user.createIndex({ "userMetadata.$**" : 1 }, {})
-----
-====
-
-The `@WildcardIndex` annotation allows a declarative index setup that can used either with a document type or property.
-
-If placed on a type that is a root level domain entity (one annotated with `@Document`) , the index resolver will create a
-wildcard index for it.
-
-.Wildcard index on domain type
-====
-[source,java]
-----
-@Document
-@WildcardIndexed
-public class Product {
- // …
-}
-----
-[source,javascript]
-----
-db.product.createIndex({ "$**" : 1 },{})
-----
-====
-
-The `wildcardProjection` can be used to specify keys to in-/exclude in the index.
-
-.Wildcard index with `wildcardProjection`
-====
-[source,java]
-----
-@Document
-@WildcardIndexed(wildcardProjection = "{ 'userMetadata.age' : 0 }")
-public class User {
- private @Id String id;
- private UserMetadata userMetadata;
-}
-----
-[source,javascript]
-----
-db.user.createIndex(
- { "$**" : 1 },
- { "wildcardProjection" :
- { "userMetadata.age" : 0 }
- }
-)
-----
-====
-
-Wildcard indexes can also be expressed by adding the annotation directly to the field.
-Please note that `wildcardProjection` is not allowed on nested paths such as properties.
-Projections on types annotated with `@WildcardIndexed` are omitted during index creation.
-
-.Wildcard index on property
-====
-[source,java]
-----
-@Document
-public class User {
- private @Id String id;
-
- @WildcardIndexed
- private UserMetadata userMetadata;
-}
-----
-[source,javascript]
-----
-db.user.createIndex({ "userMetadata.$**" : 1 }, {})
-----
-====
-
-[[mapping-usage-indexes.text-index]]
-=== Text Indexes
-
-NOTE: The text index feature is disabled by default for MongoDB v.2.4.
-
-Creating a text index allows accumulating several fields into a searchable full-text index.
-It is only possible to have one text index per collection, so all fields marked with `@TextIndexed` are combined into this index.
-Properties can be weighted to influence the document score for ranking results.
-The default language for the text index is English.To change the default language, set the `language` attribute to whichever language you want (for example,`@Document(language="spanish")`).
-Using a property called `language` or `@Language` lets you define a language override on a per-document base.
-The following example shows how to created a text index and set the language to Spanish:
-
-.Example Text Index Usage
-====
-[source,java]
-----
-@Document(language = "spanish")
-class SomeEntity {
-
- @TextIndexed String foo;
-
- @Language String lang;
-
- Nested nested;
-}
-
-class Nested {
-
- @TextIndexed(weight=5) String bar;
- String roo;
-}
-----
-====
-
-include::document-references.adoc[]
-
[[mapping-usage-events]]
=== Mapping Framework Events
-Events are fired throughout the lifecycle of the mapping process. This is described in the <> section.
+Events are fired throughout the lifecycle of the mapping process. This is described in the xref:mongodb/mapping/lifecycle-events.adoc[Lifecycle Events] section.
Declaring these beans in your Spring ApplicationContext causes them to be invoked whenever the event is dispatched.
-
-include::unwrapping-entities.adoc[]
-
-include::mongo-custom-conversions.adoc[]
-include::mongo-property-converters.adoc[]
diff --git a/src/main/asciidoc/reference/mongo-property-converters.adoc b/src/main/antora/modules/ROOT/pages/mongodb/mapping/property-converters.adoc
similarity index 83%
rename from src/main/asciidoc/reference/mongo-property-converters.adoc
rename to src/main/antora/modules/ROOT/pages/mongodb/mapping/property-converters.adoc
index c6d9fde32..b7a5054d6 100644
--- a/src/main/asciidoc/reference/mongo-property-converters.adoc
+++ b/src/main/antora/modules/ROOT/pages/mongodb/mapping/property-converters.adoc
@@ -1,7 +1,7 @@
[[mongo.property-converters]]
-== Property Converters - Mapping specific fields
+= Property Converters
-While <> already offers ways to influence the conversion and representation of certain types within the target store, it has limitations when only certain values or properties of a particular type should be considered for conversion.
+While xref:mongodb/mapping/custom-conversions.adoc[type-based conversion] already offers ways to influence the conversion and representation of certain types within the target store, it has limitations when only certain values or properties of a particular type should be considered for conversion.
Property-based converters allow configuring conversion rules on a per-property basis, either declaratively (via `@ValueConverter`) or programmatically (by registering a `PropertyValueConverter` for a specific property).
A `PropertyValueConverter` can transform a given value into its store representation (write) and back (read) as the following listing shows.
@@ -35,7 +35,7 @@ You can use `PropertyValueConverterFactory.beanFactoryAware(…)` to obtain a `P
You can change the default behavior through `ConverterConfiguration`.
[[mongo.property-converters.declarative]]
-=== Declarative Value Converter
+== Declarative Value Converter
The most straight forward usage of a `PropertyValueConverter` is by annotating properties with the `@ValueConverter` annotation that defines the converter type:
@@ -52,7 +52,7 @@ class Person {
====
[[mongo.property-converters.programmatic]]
-=== Programmatic Value Converter Registration
+== Programmatic Value Converter Registration
Programmatic registration registers `PropertyValueConverter` instances for properties within an entity model by using a `PropertyValueConverterRegistrar`, as the following example shows.
The difference between declarative registration and programmatic registration is that programmatic registration happens entirely outside of the entity model.
@@ -78,17 +78,10 @@ registrar.registerConverter(Person.class, Person::getSsn())
WARNING: Dot notation (such as `registerConverter(Person.class, "address.street", …)`) for nagivating across properties into subdocuments is *not* supported when registering converters.
-[[mongo.property-converters.value-conversions]]
-=== MongoDB property value conversions
+TIP: `MongoValueConverter` offers a pre-typed `PropertyValueConverter` interface that uses `MongoConversionContext`.
-The preceding sections outlined the purpose an overall structure of `PropertyValueConverters`.
-This section focuses on MongoDB specific aspects.
-
-==== MongoValueConverter and MongoConversionContext
-
-`MongoValueConverter` offers a pre-typed `PropertyValueConverter` interface that uses `MongoConversionContext`.
-
-==== MongoCustomConversions configuration
+[[mongocustomconversions-configuration]]
+== MongoCustomConversions configuration
By default, `MongoCustomConversions` can handle declarative value converters, depending on the configured `PropertyValueConverterFactory`.
`MongoConverterConfigurationAdapter` helps to set up programmatic value conversions or define the `PropertyValueConverterFactory` to be used.
diff --git a/src/main/asciidoc/reference/unwrapping-entities.adoc b/src/main/antora/modules/ROOT/pages/mongodb/mapping/unwrapping-entities.adoc
similarity index 93%
rename from src/main/asciidoc/reference/unwrapping-entities.adoc
rename to src/main/antora/modules/ROOT/pages/mongodb/mapping/unwrapping-entities.adoc
index 532fc0282..cff702f17 100644
--- a/src/main/asciidoc/reference/unwrapping-entities.adoc
+++ b/src/main/antora/modules/ROOT/pages/mongodb/mapping/unwrapping-entities.adoc
@@ -1,10 +1,10 @@
[[unwrapped-entities]]
-== Unwrapping Types
+= Unwrapping Types
Unwrapped entities are used to design value objects in your Java domain model whose properties are flattened out into the parent's MongoDB Document.
[[unwrapped-entities.mapping]]
-=== Unwrapped Types Mapping
+== Unwrapped Types Mapping
Consider the following domain model where `User.name` is annotated with `@Unwrapped`.
The `@Unwrapped` annotation signals that all properties of `UserName` should be flattened out into the `user` document that owns the `name` property.
@@ -53,7 +53,7 @@ However, those must not be, nor contain unwrapped fields themselves.
====
[[unwrapped-entities.mapping.field-names]]
-=== Unwrapped Types field names
+== Unwrapped Types field names
A value object can be unwrapped multiple times by using the optional `prefix` attribute of the `@Unwrapped` annotation.
By dosing so the chosen prefix is prepended to each property or `@Field("…")` name in the unwrapped object.
@@ -136,7 +136,7 @@ public class UserName {
====
[[unwrapped-entities.queries]]
-=== Query on Unwrapped Objects
+== Query on Unwrapped Objects
Defining queries on unwrapped properties is possible on type- as well as field-level as the provided `Criteria` is matched against the domain type.
Prefixes and potential custom field names will be considered when rendering the actual query.
@@ -179,7 +179,7 @@ db.collection.find({
====
[[unwrapped-entities.queries.sort]]
-==== Sort by unwrapped field.
+=== Sort by unwrapped field.
Fields of unwrapped objects can be used for sorting via their property path as shown in the sample below.
@@ -205,7 +205,7 @@ Though possible, using the unwrapped object itself as sort criteria includes all
====
[[unwrapped-entities.queries.project]]
-==== Field projection on unwrapped objects
+=== Field projection on unwrapped objects
Fields of unwrapped objects can be subject for projection either as a whole or via single fields as shown in the samples below.
@@ -253,13 +253,13 @@ db.collection.find({
====
[[unwrapped-entities.queries.by-example]]
-==== Query By Example on unwrapped object.
+=== Query By Example on unwrapped object.
Unwrapped objects can be used within an `Example` probe just as any other type.
-Please review the <> section, to learn more about this feature.
+Please review the xref:mongodb/template-query-operations.adoc#mongo.query-by-example[Query By Example] section, to learn more about this feature.
[[unwrapped-entities.queries.repository]]
-==== Repository Queries on unwrapped objects.
+=== Repository Queries on unwrapped objects.
The `Repository` abstraction allows deriving queries on fields of unwrapped objects as well as the entire object.
@@ -284,7 +284,7 @@ Index creation for unwrapped objects is suspended even if the repository `create
====
[[unwrapped-entities.update]]
-=== Update on Unwrapped Objects
+== Update on Unwrapped Objects
Unwrapped objects can be updated as any other object that is part of the domain model.
The mapping layer takes care of flattening structures into their surroundings.
@@ -338,14 +338,14 @@ db.collection.update({
====
[[unwrapped-entities.aggregations]]
-=== Aggregations on Unwrapped Objects
+== Aggregations on Unwrapped Objects
-The <> will attempt to map unwrapped values of typed aggregations.
+The xref:mongodb/aggregation-framework.adoc[Aggregation Framework] will attempt to map unwrapped values of typed aggregations.
Please make sure to work with the property path including the wrapper object when referencing one of its values.
Other than that no special action is required.
[[unwrapped-entities.indexes]]
-=== Index on Unwrapped Objects
+== Index on Unwrapped Objects
It is possible to attach the `@Indexed` annotation to properties of an unwrapped type just as it is done with regular objects.
It is not possible to use `@Indexed` along with the `@Unwrapped` annotation on the owning property.
diff --git a/src/main/asciidoc/reference/mongo-encryption.adoc b/src/main/antora/modules/ROOT/pages/mongodb/mongo-encryption.adoc
similarity index 90%
rename from src/main/asciidoc/reference/mongo-encryption.adoc
rename to src/main/antora/modules/ROOT/pages/mongodb/mongo-encryption.adoc
index 41c470f76..6487f39ba 100644
--- a/src/main/asciidoc/reference/mongo-encryption.adoc
+++ b/src/main/antora/modules/ROOT/pages/mongodb/mongo-encryption.adoc
@@ -1,5 +1,5 @@
[[mongo.encryption]]
-= Client Side Field Level Encryption (CSFLE)
+= Encryption (CSFLE)
Client Side Encryption is a feature that encrypts data in your application before it is sent to MongoDB.
We recommend you get familiar with the concepts, ideally from the https://www.mongodb.com/docs/manual/core/csfle/[MongoDB Documentation] to learn more about its capabilities and restrictions before you continue applying Encryption through Spring Data.
@@ -15,9 +15,9 @@ Specific data types require deterministic encryption to preserve equality compar
== Automatic Encryption
MongoDB supports https://www.mongodb.com/docs/manual/core/csfle/[Client-Side Field Level Encryption] out of the box using the MongoDB driver with its Automatic Encryption feature.
-Automatic Encryption requires a <> that allows to perform encrypted read and write operations without the need to provide an explicit en-/decryption step.
+Automatic Encryption requires a xref:mongodb/template-collection-schema.adoc[JSON Schema] that allows to perform encrypted read and write operations without the need to provide an explicit en-/decryption step.
-Please refer to the <> section for more information on defining a JSON Schema that holds encryption information.
+Please refer to the xref:mongodb/template-collection-schema.adoc#mongo.jsonSchema.encrypted-fields[JSON Schema] section for more information on defining a JSON Schema that holds encryption information.
To make use of a the `MongoJsonSchema` it needs to be combined with `AutoEncryptionSettings` which can be done eg. via a `MongoClientSettingsBuilderCustomizer`.
@@ -50,7 +50,7 @@ MongoClientSettingsBuilderCustomizer customizer(MappingContext mappingContext) {
== Explicit Encryption
Explicit encryption uses the MongoDB driver's encryption library (`org.mongodb:mongodb-crypt`) to perform encryption and decryption tasks.
-The `@ExplicitEncrypted` annotation is a combination of the `@Encrypted` annotation used for <> and a <>.
+The `@ExplicitEncrypted` annotation is a combination of the `@Encrypted` annotation used for xref:mongodb/template-collection-schema.adoc#mongo.jsonSchema.encrypted-fields[JSON Schema creation] and a xref:mongodb/mapping/property-converters.adoc[Property Converter].
In other words, `@ExplicitEncrypted` uses existing building blocks to combine them for simplified explicit encryption support.
[NOTE]
@@ -112,7 +112,7 @@ Fields cannot be used in queries/aggregations.
By default, the `@ExplicitEncrypted(value=…)` attribute references a `MongoEncryptionConverter`.
It is possible to change the default implementation and exchange it with any `PropertyValueConverter` implementation by providing the according type reference.
-To learn more about custom `PropertyValueConverters` and the required configuration, please refer to the <> section.
+To learn more about custom `PropertyValueConverters` and the required configuration, please refer to the xref:mongodb/mapping/property-converters.adoc[Property Converters - Mapping specific fields] section.
[[mongo.encryption.explicit-setup]]
=== MongoEncryptionConverter Setup
diff --git a/src/main/antora/modules/ROOT/pages/mongodb/mongo-group.adoc b/src/main/antora/modules/ROOT/pages/mongodb/mongo-group.adoc
new file mode 100644
index 000000000..e8265b983
--- /dev/null
+++ b/src/main/antora/modules/ROOT/pages/mongodb/mongo-group.adoc
@@ -0,0 +1,97 @@
+[[mongo.group]]
+= Group Operations
+
+As an alternative to using Map-Reduce to perform data aggregation, you can use the https://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 running of group operations. It can convert the results of the group operation to a POJO and also integrates with Spring's https://docs.spring.io/spring-framework/docs/{springVersion}/reference/html/core.html#resources[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.
+
+[[mongo.group.example]]
+== Example Usage
+
+In order to understand how group operations work the following example is used, which is somewhat artificial. For a more realistic example consult the book 'MongoDB - The definitive guide'. A collection named `group_test_collection` created with the following rows.
+
+[source]
+----
+{ "_id" : ObjectId("4ec1d25d41421e2015da64f1"), "x" : 1 }
+{ "_id" : ObjectId("4ec1d25d41421e2015da64f2"), "x" : 1 }
+{ "_id" : ObjectId("4ec1d25d41421e2015da64f3"), "x" : 2 }
+{ "_id" : ObjectId("4ec1d25d41421e2015da64f4"), "x" : 3 }
+{ "_id" : ObjectId("4ec1d25d41421e2015da64f5"), "x" : 3 }
+{ "_id" : ObjectId("4ec1d25d41421e2015da64f6"), "x" : 3 }
+----
+
+We would like to group by the only field in each row, the `x` field and aggregate the number of times each specific value of `x` occurs. To do this we need to create an initial document that contains our count variable and also a reduce function which will increment it each time it is encountered. The Java code to run the group operation is shown below
+
+[source,java]
+----
+GroupByResults results = mongoTemplate.group("group_test_collection",
+ GroupBy.key("x").initialDocument("{ count: 0 }").reduceFunction("function(doc, prev) { prev.count += 1 }"),
+ XObject.class);
+----
+
+The first argument is the name of the collection to run the group operation over, the second is a fluent API that specifies properties of the group operation via a `GroupBy` class. In this example we are using just the `intialDocument` and `reduceFunction` methods. You can also specify a key-function, as well as a finalizer as part of the fluent API. If you have multiple keys to group by, you can pass in a comma separated list of keys.
+
+The raw results of the group operation is a JSON document that looks like this
+
+[source]
+----
+{
+ "retval" : [ { "x" : 1.0 , "count" : 2.0} ,
+ { "x" : 2.0 , "count" : 1.0} ,
+ { "x" : 3.0 , "count" : 3.0} ] ,
+ "count" : 6.0 ,
+ "keys" : 3 ,
+ "ok" : 1.0
+}
+----
+
+The document under the "retval" field is mapped onto the third argument in the group method, in this case XObject which is shown below.
+
+[source,java]
+----
+public class XObject {
+
+ private float x;
+
+ private float count;
+
+
+ public float getX() {
+ return x;
+ }
+
+ public void setX(float x) {
+ this.x = x;
+ }
+
+ public float getCount() {
+ return count;
+ }
+
+ public void setCount(float count) {
+ this.count = count;
+ }
+
+ @Override
+ public String toString() {
+ return "XObject [x=" + x + " count = " + count + "]";
+ }
+}
+----
+
+You can also obtain the raw result as a `Document` by calling the method `getRawResults` on the `GroupByResults` class.
+
+There is an additional method overload of the group method on `MongoOperations` which lets you specify a `Criteria` object for selecting a subset of the rows. An example which uses a `Criteria` object, with some syntax sugar using static imports, as well as referencing a key-function and reduce function javascript files via a Spring Resource string is shown below.
+
+[source]
+----
+import static org.springframework.data.mongodb.core.mapreduce.GroupBy.keyFunction;
+import static org.springframework.data.mongodb.core.query.Criteria.where;
+
+GroupByResults results = mongoTemplate.group(where("x").gt(0),
+ "group_test_collection",
+ keyFunction("classpath:keyFunction.js").initialDocument("{ count: 0 }").reduceFunction("classpath:groupReduce.js"), XObject.class);
+----
+
+include:../:aggregation-framework.adoc[]
+
diff --git a/src/main/antora/modules/ROOT/pages/mongodb/mongo-mapreduce.adoc b/src/main/antora/modules/ROOT/pages/mongodb/mongo-mapreduce.adoc
new file mode 100644
index 000000000..bfccec44f
--- /dev/null
+++ b/src/main/antora/modules/ROOT/pages/mongodb/mongo-mapreduce.adoc
@@ -0,0 +1,127 @@
+[[mongo.mapreduce]]
+= Map-Reduce Operations
+
+You can query MongoDB by using Map-Reduce, which is useful for batch processing, for data aggregation, and for when the query language does not fulfill your needs.
+
+Spring provides integration with MongoDB's Map-Reduce by providing methods on `MongoOperations` to simplify the creation and running of Map-Reduce operations.It can convert the results of a Map-Reduce operation to a POJO and integrates with Spring's link:{springDocsUrl}/core.html#resources[Resource abstraction].This lets you place your JavaScript files on the file system, classpath, HTTP server, or any other Spring Resource implementation and then reference the JavaScript resources through an easy URI style syntax -- for example, `classpath:reduce.js;`.Externalizing JavaScript code in files is 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.
+
+[[mongo.mapreduce.example]]
+== Example Usage
+
+To understand how to perform Map-Reduce operations, we use an example from the book, _MongoDB - The Definitive Guide_ footnote:[Kristina Chodorow. _MongoDB - The Definitive Guide_. O'Reilly Media, 2013].In this example, we create three documents that have the values [a,b], [b,c], and [c,d], respectively.The values in each document are associated with the key, 'x', as the following example shows (assume these documents are in a collection named `jmr1`):
+
+[source]
+----
+{ "_id" : ObjectId("4e5ff893c0277826074ec533"), "x" : [ "a", "b" ] }
+{ "_id" : ObjectId("4e5ff893c0277826074ec534"), "x" : [ "b", "c" ] }
+{ "_id" : ObjectId("4e5ff893c0277826074ec535"), "x" : [ "c", "d" ] }
+----
+
+The following map function counts the occurrence of each letter in the array for each document:
+
+[source,java]
+----
+function () {
+ for (var i = 0; i < this.x.length; i++) {
+ emit(this.x[i], 1);
+ }
+}
+----
+
+The follwing reduce function sums up the occurrence of each letter across all the documents:
+
+[source,java]
+----
+function (key, values) {
+ var sum = 0;
+ for (var i = 0; i < values.length; i++)
+ sum += values[i];
+ return sum;
+}
+----
+
+Running the preceding functions result in the following collection:
+
+[source]
+----
+{ "_id" : "a", "value" : 1 }
+{ "_id" : "b", "value" : 2 }
+{ "_id" : "c", "value" : 2 }
+{ "_id" : "d", "value" : 1 }
+----
+
+Assuming that the map and reduce functions are located in `map.js` and `reduce.js` and bundled in your jar so they are available on the classpath, you can run a Map-Reduce operation as follows:
+
+[source,java]
+----
+MapReduceResults results = mongoOperations.mapReduce("jmr1", "classpath:map.js", "classpath:reduce.js", ValueObject.class);
+for (ValueObject valueObject : results) {
+ System.out.println(valueObject);
+}
+----
+
+The preceding exmaple produces the following output:
+
+[source]
+----
+ValueObject [id=a, value=1.0]
+ValueObject [id=b, value=2.0]
+ValueObject [id=c, value=2.0]
+ValueObject [id=d, value=1.0]
+----
+
+The `MapReduceResults` class implements `Iterable` and provides access to the raw output and timing and count statistics.The following listing shows the `ValueObject` class:
+
+[source,java]
+----
+public class ValueObject {
+
+ private String id;
+ private float value;
+
+ public String getId() {
+ return id;
+ }
+
+ public float getValue() {
+ return value;
+ }
+
+ public void setValue(float value) {
+ this.value = value;
+ }
+
+ @Override
+ public String toString() {
+ return "ValueObject [id=" + id + ", value=" + value + "]";
+ }
+}
+----
+
+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]
+----
+MapReduceResults results = mongoOperations.mapReduce("jmr1", "classpath:map.js", "classpath:reduce.js",
+ new MapReduceOptions().outputCollection("jmr1_out"), ValueObject.class);
+----
+
+There is also a static import (`import static org.springframework.data.mongodb.core.mapreduce.MapReduceOptions.options;`) that can be used to make the syntax slightly more compact, as the following example shows:
+
+[source,java]
+----
+MapReduceResults results = mongoOperations.mapReduce("jmr1", "classpath:map.js", "classpath:reduce.js",
+ options().outputCollection("jmr1_out"), ValueObject.class);
+----
+
+You can also specify a query to reduce the set of data that is fed into the Map-Reduce operation.The following example removes the document that contains [a,b] from consideration for Map-Reduce operations:
+
+[source,java]
+----
+Query query = new Query(where("x").ne(new String[] { "a", "b" }));
+MapReduceResults results = mongoOperations.mapReduce(query, "jmr1", "classpath:map.js", "classpath:reduce.js",
+ options().outputCollection("jmr1_out"), ValueObject.class);
+----
+
+Note that you can specify additional limit and sort values on the query, but you cannot skip values.
+
diff --git a/src/main/antora/modules/ROOT/pages/mongodb/mongo-server-side-scripts.adoc b/src/main/antora/modules/ROOT/pages/mongodb/mongo-server-side-scripts.adoc
new file mode 100644
index 000000000..0cac130b6
--- /dev/null
+++ b/src/main/antora/modules/ROOT/pages/mongodb/mongo-server-side-scripts.adoc
@@ -0,0 +1,28 @@
+[[mongo.server-side-scripts]]
+= Script Operations
+
+[WARNING]
+====
+https://docs.mongodb.com/master/release-notes/4.2-compatibility/[MongoDB 4.2] removed support for the `eval` command used
+by `ScriptOperations`. +
+There is no replacement for the removed functionality.
+====
+
+MongoDB allows running JavaScript functions on the server by either directly sending the script or calling a stored one. `ScriptOperations` can be accessed through `MongoTemplate` and provides basic abstraction for `JavaScript` usage. The following example shows how to us the `ScriptOperations` class:
+
+====
+[source,java]
+----
+ScriptOperations scriptOps = template.scriptOps();
+
+ExecutableMongoScript echoScript = new ExecutableMongoScript("function(x) { return x; }");
+scriptOps.execute(echoScript, "directly execute script"); <1>
+
+scriptOps.register(new NamedMongoScript("echo", echoScript)); <2>
+scriptOps.call("echo", "execute script via name"); <3>
+----
+<1> Run the script directly without storing the function on server side.
+<2> Store the script using 'echo' as its name. The given name identifies the script and allows calling it later.
+<3> Run the script with name 'echo' using the provided parameters.
+====
+
diff --git a/src/main/antora/modules/ROOT/pages/mongodb/query-by-example.adoc b/src/main/antora/modules/ROOT/pages/mongodb/query-by-example.adoc
new file mode 100644
index 000000000..93929bf91
--- /dev/null
+++ b/src/main/antora/modules/ROOT/pages/mongodb/query-by-example.adoc
@@ -0,0 +1,25 @@
+[[query-by-example.running]]
+= Running an Example
+
+TODO: move this section to the repositories documentation
+
+The following example shows how to query by example when using a repository (of `Person` objects, in this case):
+
+.Query by Example using a repository
+====
+[source, java]
+----
+public interface PersonRepository extends QueryByExampleExecutor {
+
+}
+
+public class PersonService {
+
+ @Autowired PersonRepository personRepository;
+
+ public List findPeople(Person probe) {
+ return personRepository.findAll(Example.of(probe));
+ }
+}
+----
+====
diff --git a/src/main/antora/modules/ROOT/pages/mongodb/repositories/cdi-integration.adoc b/src/main/antora/modules/ROOT/pages/mongodb/repositories/cdi-integration.adoc
new file mode 100644
index 000000000..06b2a42dc
--- /dev/null
+++ b/src/main/antora/modules/ROOT/pages/mongodb/repositories/cdi-integration.adoc
@@ -0,0 +1,38 @@
+[[mongodb.repositories.misc.cdi-integration]]
+= CDI Integration
+
+Instances of the repository interfaces are usually created by a container, and Spring is the most natural choice when working with Spring Data.
+As of version 1.3.0, Spring Data MongoDB ships with a custom CDI extension that lets you use the repository abstraction in CDI environments.
+The extension is part of the JAR.
+To activate it, drop the Spring Data MongoDB JAR into your classpath.
+You can now set up the infrastructure by implementing a CDI Producer for the `MongoTemplate`, as the following example shows:
+
+[source,java]
+----
+class MongoTemplateProducer {
+
+ @Produces
+ @ApplicationScoped
+ public MongoOperations createMongoTemplate() {
+
+ MongoDatabaseFactory factory = new SimpleMongoClientDatabaseFactory(MongoClients.create(), "database");
+ return new MongoTemplate(factory);
+ }
+}
+----
+
+The Spring Data MongoDB CDI extension picks up the `MongoTemplate` available as a CDI bean and creates a proxy for a Spring Data repository whenever a bean of a repository type is requested by the container.
+Thus, obtaining an instance of a Spring Data repository is a matter of declaring an `@Inject`-ed property, as the following example shows:
+
+[source,java]
+----
+class RepositoryClient {
+
+ @Inject
+ PersonRepository repository;
+
+ public void businessMethod() {
+ List people = repository.findAll();
+ }
+}
+----
diff --git a/src/main/antora/modules/ROOT/pages/mongodb/repositories/repositories-collation.adoc b/src/main/antora/modules/ROOT/pages/mongodb/repositories/repositories-collation.adoc
new file mode 100644
index 000000000..ebf0589eb
--- /dev/null
+++ b/src/main/antora/modules/ROOT/pages/mongodb/repositories/repositories-collation.adoc
@@ -0,0 +1,69 @@
+[[mongo.repositories.collation]]
+= Repository Collation Support
+
+Next to the xref:mongodb/collation.adoc[general Collation Support] repositories allow to define the collation for various operations.
+
+====
+[source,java]
+----
+public interface PersonRepository extends MongoRepository {
+
+ @Query(collation = "en_US") <1>
+ List findByFirstname(String firstname);
+
+ @Query(collation = "{ 'locale' : 'en_US' }") <2>
+ List findPersonByFirstname(String firstname);
+
+ @Query(collation = "?1") <3>
+ List findByFirstname(String firstname, Object collation);
+
+ @Query(collation = "{ 'locale' : '?1' }") <4>
+ List findByFirstname(String firstname, String collation);
+
+ List findByFirstname(String firstname, Collation collation); <5>
+
+ @Query(collation = "{ 'locale' : 'en_US' }")
+ List findByFirstname(String firstname, @Nullable Collation collation); <6>
+}
+----
+<1> Static collation definition resulting in `{ 'locale' : 'en_US' }`.
+<2> Static collation definition resulting in `{ 'locale' : 'en_US' }`.
+<3> Dynamic collation depending on 2nd method argument. Allowed types include `String` (eg. 'en_US'), `Locacle` (eg. Locacle.US)
+and `Document` (eg. new Document("locale", "en_US"))
+<4> Dynamic collation depending on 2nd method argument.
+<5> Apply the `Collation` method parameter to the query.
+<6> The `Collation` method parameter overrides the default `collation` from `@Query` if not null.
+
+NOTE: In case you enabled the automatic index creation for repository finder methods a potential static collation definition,
+as shown in (1) and (2), will be included when creating the index.
+
+TIP: The most specifc `Collation` outrules potentially defined others. Which means Method argument over query method annotation over domain type annotation.
+====
+
+To streamline usage of collation attributes throughout the codebase it is also possible to use the `@Collation` annotation, which serves as a meta annotation for the ones mentioned above.
+The same rules and locations apply, plus, direct usage of `@Collation` supersedes any collation values defined on `@Query` and other annotations.
+Which means, if a collation is declared via `@Query` and additionally via `@Collation`, then the one from `@Collation` is picked.
+
+.Using `@Collation`
+====
+[source,java]
+----
+@Collation("en_US") <1>
+class Game {
+ // ...
+}
+
+interface GameRepository extends Repository {
+
+ @Collation("en_GB") <2>
+ List findByTitle(String title);
+
+ @Collation("de_AT") <3>
+ @Query(collation="en_GB")
+ List findByDescriptionContaining(String keyword);
+}
+----
+<1> Instead of `@Document(collation=...)`.
+<2> Instead of `@Query(collation=...)`.
+<3> Favors `@Collation` over meta usage.
+====
diff --git a/src/main/antora/modules/ROOT/pages/mongodb/repositories/repositories-index-hints.adoc b/src/main/antora/modules/ROOT/pages/mongodb/repositories/repositories-index-hints.adoc
new file mode 100644
index 000000000..555bbf96e
--- /dev/null
+++ b/src/main/antora/modules/ROOT/pages/mongodb/repositories/repositories-index-hints.adoc
@@ -0,0 +1,20 @@
+[[mongodb.repositories.index-hint]]
+= Index Hints
+
+The `@Hint` annotation allows to override MongoDB's default index selection and forces the database to use the specified index instead.
+
+.Example of index hints
+====
+[source,java]
+----
+@Hint("lastname-idx") <1>
+List findByLastname(String lastname);
+
+@Query(value = "{ 'firstname' : ?0 }", hint = "firstname-idx") <2>
+List findByFirstname(String firstname);
+----
+<1> Use the index with name `lastname-idx`.
+<2> The `@Query` annotation defines the `hint` alias which is equivalent to adding the `@Hint` annotation.
+====
+
+For more information about index creation please refer to the xref:mongodb/template-collection-management.adoc[Collection Management] section.
diff --git a/src/main/antora/modules/ROOT/pages/mongodb/repositories/repositories.adoc b/src/main/antora/modules/ROOT/pages/mongodb/repositories/repositories.adoc
new file mode 100644
index 000000000..97149d3e6
--- /dev/null
+++ b/src/main/antora/modules/ROOT/pages/mongodb/repositories/repositories.adoc
@@ -0,0 +1,1173 @@
+[[mongo.repositories]]
+= MongoDB Repositories
+
+[[mongo-repo-intro]]
+This chapter points out the specialties for repository support for MongoDB.
+This chapter builds on the core repository support explained in xref:repositories/core-concepts.adoc[core concepts].
+You should have a sound understanding of the basic concepts explained there.
+
+[[mongo-repo-usage]]
+== Usage
+
+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
+====
+[source,java]
+----
+public class Person {
+
+ @Id
+ private String id;
+ private String firstname;
+ private String lastname;
+ private Address address;
+
+ // … getters and setters omitted
+}
+----
+====
+
+Note that the domain type shown in the preceding example has a property named `id` of type `String`.The default serialization mechanism used in `MongoTemplate` (which backs the repository support) regards properties named `id` as the document ID.
+Currently, we support `String`, `ObjectId`, and `BigInteger` as ID types.
+Please see xref:mongodb/template-id-handling.adoc#mongo-template.id-handling[ID mapping] for more information about on how the `id` field is handled in the mapping layer.
+
+Now that we have a domain object, we can define an interface that uses it, as follows:
+
+.Basic repository interface to persist Person entities
+[tabs]
+======
+Imperative::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
+----
+public interface PersonRepository extends PagingAndSortingRepository {
+
+ // additional custom query methods go here
+}
+----
+
+Reactive::
++
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+public interface PersonRepository extends ReactiveSortingRepository {
+
+ // additional custom query methods go here
+}
+----
+======
+
+To start using the repository, use the `@EnableMongoRepositories` annotation.
+That annotation carries the same attributes as the namespace element.
+If no base package is configured, the infrastructure scans the package of the annotated configuration class.
+The following example shows how to configuration your application to use MongoDB repositories:
+
+[tabs]
+======
+Imperative::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
+----
+@Configuration
+@EnableMongoRepositories("com.acme.*.repositories")
+class ApplicationConfig extends AbstractMongoClientConfiguration {
+
+ @Override
+ protected String getDatabaseName() {
+ return "e-store";
+ }
+
+ @Override
+ protected String getMappingBasePackage() {
+ return "com.acme.*.repositories";
+ }
+}
+----
+
+Reactive::
++
+====
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+@Configuration
+@EnableReactiveMongoRepositories("com.acme.*.repositories")
+class ApplicationConfig extends AbstractReactiveMongoConfiguration {
+
+ @Override
+ protected String getDatabaseName() {
+ return "e-store";
+ }
+
+ @Override
+ protected String getMappingBasePackage() {
+ return "com.acme.*.repositories";
+ }
+}
+----
+
+NOTE: MongoDB uses two different drivers for imperative (synchronous/blocking) and reactive (non-blocking) data access. You must create a connection by using the Reactive Streams driver to provide the required infrastructure for Spring Data's Reactive MongoDB support. Consequently, you must provide a separate configuration for MongoDB's Reactive Streams driver. Note that your application operates on two different connections if you use reactive and blocking Spring Data MongoDB templates and repositories.
+====
+
+XML::
++
+[source,xml,indent=0,subs="verbatim,quotes",role="third"]
+----
+
+
+
+
+
+
+
+
+
+
+
+
+
+----
+======
+
+This namespace element causes the base packages to be scanned for interfaces that extend `MongoRepository` and create Spring beans for each one found.
+By default, the repositories get a `MongoTemplate` Spring bean wired that is called `mongoTemplate`, so you only need to configure `mongo-template-ref` explicitly if you deviate from this convention.
+
+Because our domain repository extends `PagingAndSortingRepository`, it provides you with methods for paginated and sorted access to the entities.
+In the case of reactive repositories only `ReactiveSortingRepository` is available since the notion of a `Page` is not applicable.
+However finder methods still accept a `Sort` and `Limit` parameter.
+
+[NOTE]
+====
+The reactive space offers various reactive composition libraries. The most common libraries are https://github.com/ReactiveX/RxJava[RxJava] and https://projectreactor.io/[Project Reactor].
+
+Spring Data MongoDB is built on top of the https://mongodb.github.io/mongo-java-driver-reactivestreams/[MongoDB Reactive Streams] driver, to provide maximal interoperability by relying on the https://www.reactive-streams.org/[Reactive Streams] initiative. Static APIs, such as `ReactiveMongoOperations`, are provided by using Project Reactor's `Flux` and `Mono` types. Project Reactor offers various adapters to convert reactive wrapper types (`Flux` to `Observable` and vice versa), but conversion can easily clutter your code.
+
+Spring Data's Reactive Repository abstraction is a dynamic API, mostly defined by you and your requirements as you declare query methods. Reactive MongoDB repositories can be implemented by using either RxJava or Project Reactor wrapper types by extending from one of the following library-specific repository interfaces:
+
+* `ReactiveCrudRepository`
+* `ReactiveSortingRepository`
+* `RxJava3CrudRepository`
+* `RxJava3SortingRepository`
+
+Spring Data converts reactive wrapper types behind the scenes so that you can stick to your favorite composition library.
+====
+
+In case you want to obtain methods for basic CRUD operations also add the `CrudRepository` interface.
+Working with the repository instance is just a matter of dependency injecting it into a client .
+Consequently, accessing the second page of `Person` objects at a page size of 10 would resemble the following code:
+
+.Paging access to Person entities
+[tabs]
+======
+Imperative::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
+----
+@ExtendWith(SpringExtension.class)
+@ContextConfiguration
+class PersonRepositoryTests {
+
+ @Autowired PersonRepository repository;
+
+ @Test
+ void readsFirstPageCorrectly() {
+
+ Page persons = repository.findAll(PageRequest.of(0, 10));
+ assertThat(persons.isFirstPage()).isTrue();
+ }
+}
+----
+
+Reactive::
++
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+@ExtendWith(SpringExtension.class)
+@ContextConfiguration
+class PersonRepositoryTests {
+
+ @Autowired PersonRepository repository;
+
+ @Test
+ void readsFirstPageCorrectly() {
+
+ Flux persons = repository.findAll(Sort.unsorted(), Limit.of(10));
+
+ persons.as(StepVerifer::create)
+ .expectNextCount(10)
+ .verifyComplete();
+ }
+}
+----
+======
+
+The preceding example creates an application context with Spring's unit test support, which performs annotation-based dependency injection into test cases.
+Inside the test method, we use the repository to query the datastore.
+We hand the repository a `PageRequest` instance that requests the first page of `Person` objects at a page size of 10.
+
+[[mongodb.repositories.queries]]
+== Query Methods
+
+Most of the data access operations you usually trigger on a repository result in a query being executed against the MongoDB databases.
+Defining such a query is a matter of declaring a method on the repository interface, as the following example shows:
+
+.PersonRepository with query methods
+[tabs]
+======
+Imperative::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
+----
+public interface PersonRepository extends PagingAndSortingRepository {
+
+ List findByLastname(String lastname); <1>
+
+ Page findByFirstname(String firstname, Pageable pageable); <2>
+
+ Person findByShippingAddresses(Address address); <3>
+
+ Person findFirstByLastname(String lastname); <4>
+
+ Stream findAllBy(); <5>
+}
+----
+<1> The `findByLastname` method shows a query for all people with the given last name.
+The query is derived by parsing the method name for constraints that can be concatenated with `And` and `Or`.
+Thus, the method name results in a query expression of `{"lastname" : lastname}`.
+<2> Applies pagination to a query.
+You can equip your method signature with a `Pageable` parameter and let the method return a `Page` instance and Spring Data automatically pages the query accordingly.
+<3> Shows that you can query based on properties that are not primitive types.
+Throws `IncorrectResultSizeDataAccessException` if more than one match is found.
+<4> Uses the `First` keyword to restrict the query to only the first result.
+Unlike <3>, this method does not throw an exception if more than one match is found.
+<5> Uses a Java 8 `Stream` that reads and converts individual elements while iterating the stream.
+
+Reactive::
++
+====
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+public interface ReactivePersonRepository extends ReactiveSortingRepository {
+
+ Flux findByFirstname(String firstname); <1>
+
+ Flux findByFirstname(Publisher firstname); <2>
+
+ Flux findByFirstnameOrderByLastname(String firstname, Pageable pageable); <3>
+
+ Mono findByFirstnameAndLastname(String firstname, String lastname); <4>
+
+ Mono findFirstByLastname(String lastname); <5>
+}
+----
+<1> The method shows a query for all people with the given `lastname`. The query is derived by parsing the method name for constraints that can be concatenated with `And` and `Or`. Thus, the method name results in a query expression of `{"lastname" : lastname}`.
+<2> The method shows a query for all people with the given `firstname` once the `firstname` is emitted by the given `Publisher`.
+<3> Use `Pageable` to pass offset and sorting parameters to the database.
+<4> Find a single entity for the given criteria. It completes with `IncorrectResultSizeDataAccessException` on non-unique results.
+<5> Unless <4>, the first entity is always emitted even if the query yields more result documents.
+
+WARNING: The `Page` return type (as in `Mono`) is not supported by reactive repositories.
+
+It is possible to use `Pageable` in derived finder methods, to pass on `sort`, `limit` and `offset` parameters to the query to reduce load and network traffic.
+The returned `Flux` will only emit data within the declared range.
+
+[source,java]
+----
+Pageable page = PageRequest.of(1, 10, Sort.by("lastname"));
+Flux persons = repository.findByFirstnameOrderByLastname("luke", page);
+----
+====
+======
+
+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:
+
+[cols="1,2,3",options="header"]
+.Supported keywords for query methods
+|===
+| Keyword
+| Sample
+| Logical result
+
+| `After`
+| `findByBirthdateAfter(Date date)`
+| `{"birthdate" : {"$gt" : date}}`
+
+| `GreaterThan`
+| `findByAgeGreaterThan(int age)`
+| `{"age" : {"$gt" : age}}`
+
+| `GreaterThanEqual`
+| `findByAgeGreaterThanEqual(int age)`
+| `{"age" : {"$gte" : age}}`
+
+| `Before`
+| `findByBirthdateBefore(Date date)`
+| `{"birthdate" : {"$lt" : date}}`
+
+| `LessThan`
+| `findByAgeLessThan(int age)`
+| `{"age" : {"$lt" : age}}`
+
+| `LessThanEqual`
+| `findByAgeLessThanEqual(int age)`
+| `{"age" : {"$lte" : age}}`
+
+| `Between`
+| `findByAgeBetween(int from, int to)` +
+`findByAgeBetween(Range range)`
+| `{"age" : {"$gt" : from, "$lt" : to}}` +
+lower / upper bounds (`$gt` / `$gte` & `$lt` / `$lte`) according to `Range`
+
+| `In`
+| `findByAgeIn(Collection ages)`
+| `{"age" : {"$in" : [ages...]}}`
+
+| `NotIn`
+| `findByAgeNotIn(Collection ages)`
+| `{"age" : {"$nin" : [ages...]}}`
+
+| `IsNotNull`, `NotNull`
+| `findByFirstnameNotNull()`
+| `{"firstname" : {"$ne" : null}}`
+
+| `IsNull`, `Null`
+| `findByFirstnameNull()`
+| `{"firstname" : null}`
+
+| `Like`, `StartingWith`, `EndingWith`
+| `findByFirstnameLike(String name)`
+| `{"firstname" : name} (name as regex)`
+
+| `NotLike`, `IsNotLike`
+| `findByFirstnameNotLike(String name)`
+| `{"firstname" : { "$not" : name }} (name as regex)`
+
+| `Containing` on String
+| `findByFirstnameContaining(String name)`
+| `{"firstname" : name} (name as regex)`
+
+| `NotContaining` on String
+| `findByFirstnameNotContaining(String name)`
+| `{"firstname" : { "$not" : name}} (name as regex)`
+
+| `Containing` on Collection
+| `findByAddressesContaining(Address address)`
+| `{"addresses" : { "$in" : address}}`
+
+| `NotContaining` on Collection
+| `findByAddressesNotContaining(Address address)`
+| `{"addresses" : { "$not" : { "$in" : address}}}`
+
+| `Regex`
+| `findByFirstnameRegex(String firstname)`
+| `{"firstname" : {"$regex" : firstname }}`
+
+| `(No keyword)`
+| `findByFirstname(String name)`
+| `{"firstname" : name}`
+
+| `Not`
+| `findByFirstnameNot(String name)`
+| `{"firstname" : {"$ne" : name}}`
+
+| `Near`
+| `findByLocationNear(Point point)`
+| `{"location" : {"$near" : [x,y]}}`
+
+| `Near`
+| `findByLocationNear(Point point, Distance max)`
+| `{"location" : {"$near" : [x,y], "$maxDistance" : max}}`
+
+| `Near`
+| `findByLocationNear(Point point, Distance min, Distance max)`
+| `{"location" : {"$near" : [x,y], "$minDistance" : min, "$maxDistance" : max}}`
+
+| `Within`
+| `findByLocationWithin(Circle circle)`
+| `{"location" : {"$geoWithin" : {"$center" : [ [x, y], distance]}}}`
+
+| `Within`
+| `findByLocationWithin(Box box)`
+| `{"location" : {"$geoWithin" : {"$box" : [ [x1, y1], x2, y2]}}}`
+
+| `IsTrue`, `True`
+| `findByActiveIsTrue()`
+| `{"active" : true}`
+
+| `IsFalse`, `False`
+| `findByActiveIsFalse()`
+| `{"active" : false}`
+
+| `Exists`
+| `findByLocationExists(boolean exists)`
+| `{"location" : {"$exists" : exists }}`
+
+| `IgnoreCase`
+| `findByUsernameIgnoreCase(String username)`
+| `{"username" : {"$regex" : "^username$", "$options" : "i" }}`
+|===
+
+NOTE: If the property criterion compares a document, the order of the fields and exact equality in the document matters.
+
+[[mongodb.repositories.queries.geo-spatial]]
+=== Geo-spatial Repository Queries
+
+As you saw in the preceding table of keywords, a few keywords trigger geo-spatial operations within a MongoDB query.
+The `Near` keyword allows some further modification, as the next few examples show.
+
+The following example shows how to define a `near` query that finds all persons with a given distance of a given point:
+
+.Advanced `Near` queries
+[tabs]
+======
+Imperative::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
+----
+public interface PersonRepository extends MongoRepository {
+
+ // { 'location' : { '$near' : [point.x, point.y], '$maxDistance' : distance}}
+ List findByLocationNear(Point location, Distance distance);
+}
+----
+
+Reactive::
++
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+interface PersonRepository extends ReactiveMongoRepository {
+
+ // { 'location' : { '$near' : [point.x, point.y], '$maxDistance' : distance}}
+ Flux findByLocationNear(Point location, Distance distance);
+}
+----
+======
+
+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`
+====
+[source,java]
+----
+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}}
+----
+====
+
+NOTE: Reactive Geo-spatial repository queries support the domain type and `GeoResult` results within a reactive wrapper type. `GeoPage` and `GeoResults` are not supported as they contradict the deferred result approach with pre-calculating the average distance. However, you can still pass in a `Pageable` argument to page results yourself.
+
+Using a `Distance` with a `Metric` causes a `$nearSphere` (instead of a plain `$near`) clause to be added.
+Beyond that, the actual distance gets calculated according to the `Metrics` used.
+
+(Note that `Metric` does not refer to metric units of measure.
+It could be miles rather than kilometers.
+Rather, `metric` refers to the concept of a system of measurement, regardless of which system you use.)
+
+NOTE: Using `@GeoSpatialIndexed(type = GeoSpatialIndexType.GEO_2DSPHERE)` on the target property forces usage of the `$nearSphere` operator.
+
+[[geo-near-queries]]
+==== Geo-near Queries
+
+Spring Data MongoDb supports geo-near queries, as the following example shows:
+
+[tabs]
+======
+Imperative::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
+----
+public 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);
+
+ // Metric: {'geoNear' : 'person', 'near' : [x, y], 'minDistance' : min,
+ // 'maxDistance' : max, 'distanceMultiplier' : metric.multiplier,
+ // 'spherical' : true }
+ GeoResults findByLocationNear(Point location, Distance min, Distance max);
+
+ // {'geoNear' : 'location', 'near' : [x, y] }
+ GeoResults findByLocationNear(Point location);
+}
+----
+
+Reactive::
++
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+interface PersonRepository extends ReactiveMongoRepository {
+
+ // {'geoNear' : 'location', 'near' : [x, y] }
+ Flux> 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 }
+ Flux> findByLocationNear(Point location, Distance distance);
+
+ // Metric: {'geoNear' : 'person', 'near' : [x, y], 'minDistance' : min,
+ // 'maxDistance' : max, 'distanceMultiplier' : metric.multiplier,
+ // 'spherical' : true }
+ Flux> findByLocationNear(Point location, Distance min, Distance max);
+
+ // {'geoNear' : 'location', 'near' : [x, y] }
+ Flux> findByLocationNear(Point location);
+}
+----
+======
+
+[[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 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:
+
+[tabs]
+======
+Imperative::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
+----
+public interface PersonRepository extends MongoRepository {
+
+ @Query("{ 'firstname' : ?0 }")
+ List findByThePersonsFirstname(String firstname);
+
+}
+----
+
+Reactive::
++
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+public interface PersonRepository extends ReactiveMongoRepository {
+
+ @Query("{ 'firstname' : ?0 }")
+ Flux findByThePersonsFirstname(String firstname);
+
+}
+----
+======
+
+The `?0` placeholder lets you substitute the value from the method arguments into the JSON query string.
+
+NOTE: `String` parameter values are escaped during the binding process, which means that it is not possible to add MongoDB specific operators through the argument.
+
+You can also use the filter property to restrict the set of properties that is mapped into the Java object, as the following example shows:
+
+[tabs]
+======
+Imperative::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
+----
+public interface PersonRepository extends MongoRepository {
+
+ @Query(value="{ 'firstname' : ?0 }", fields="{ 'firstname' : 1, 'lastname' : 1}")
+ List findByThePersonsFirstname(String firstname);
+
+}
+----
+
+Reactive::
++
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+public interface PersonRepository extends ReactiveMongoRepository {
+
+ @Query(value="{ 'firstname' : ?0 }", fields="{ 'firstname' : 1, 'lastname' : 1}")
+ Flux findByThePersonsFirstname(String firstname);
+
+}
+----
+======
+
+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.sort]]
+=== Sorting Query Method results
+
+MongoDB repositories allow various approaches to define sorting order.
+Let's take a look at the following example:
+
+.Sorting Query Results
+[tabs]
+======
+Imperative::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
+----
+public interface PersonRepository extends MongoRepository {
+
+ List findByFirstnameSortByAgeDesc(String firstname); <1>
+
+ List findByFirstname(String firstname, Sort sort); <2>
+
+ @Query(sort = "{ age : -1 }")
+ List findByFirstname(String firstname); <3>
+
+ @Query(sort = "{ age : -1 }")
+ List findByLastname(String lastname, Sort sort); <4>
+}
+----
+<1> Static sorting derived from method name. `SortByAgeDesc` results in `{ age : -1 }` for the sort parameter.
+<2> Dynamic sorting using a method argument.
+`Sort.by(DESC, "age")` creates `{ age : -1 }` for the sort parameter.
+<3> Static sorting via `Query` annotation.
+Sort parameter applied as stated in the `sort` attribute.
+<4> Default sorting via `Query` annotation combined with dynamic one via a method argument. `Sort.unsorted()`
+results in `{ age : -1 }`.
+Using `Sort.by(ASC, "age")` overrides the defaults and creates `{ age : 1 }`.
+`Sort.by
+(ASC, "firstname")` alters the default and results in `{ age : -1, firstname : 1 }`.
+
+Reactive::
++
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+public interface PersonRepository extends ReactiveMongoRepository {
+
+ Flux findByFirstnameSortByAgeDesc(String firstname);
+
+ Flux findByFirstname(String firstname, Sort sort);
+
+ @Query(sort = "{ age : -1 }")
+ Flux findByFirstname(String firstname);
+
+ @Query(sort = "{ age : -1 }")
+ Flux findByLastname(String lastname, Sort sort);
+}
+----
+======
+
+[[mongodb.repositories.queries.json-spel]]
+=== JSON-based Queries with SpEL Expressions
+
+Query strings and field definitions can be used together with SpEL expressions to create dynamic queries at runtime.
+SpEL expressions can provide predicate values and can be used to extend predicates with subdocuments.
+
+Expressions expose method arguments through an array that contains all the arguments.
+The following query uses `[0]`
+to declare the predicate value for `lastname` (which is equivalent to the `?0` parameter binding):
+
+[tabs]
+======
+Imperative::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
+----
+public interface PersonRepository extends MongoRepository {
+
+ @Query("{'lastname': ?#{[0]} }")
+ List findByQueryWithExpression(String param0);
+}
+----
+
+Reactive::
++
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+public interface PersonRepository extends ReactiveMongoRepository {
+
+ @Query("{'lastname': ?#{[0]} }")
+ Flux findByQueryWithExpression(String param0);
+}
+----
+======
+
+Expressions can be used to invoke functions, evaluate conditionals, and construct values.
+SpEL expressions used in conjunction with JSON reveal a side-effect, because Map-like declarations inside of SpEL read like JSON, as the following example shows:
+
+[tabs]
+======
+Imperative::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
+----
+public interface PersonRepository extends MongoRepository {
+
+ @Query("{'id': ?#{ [0] ? {$exists :true} : [1] }}")
+ List findByQueryWithExpressionAndNestedObject(boolean param0, String param1);
+}
+----
+
+Reactive::
++
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+public interface PersonRepository extends ReactiveMongoRepository {
+
+ @Query("{'id': ?#{ [0] ? {$exists :true} : [1] }}")
+ Flux findByQueryWithExpressionAndNestedObject(boolean param0, String param1);
+}
+----
+======
+
+WARNING: SpEL in query strings can be a powerful way to enhance queries.
+However, they can also accept a broad range of unwanted arguments.
+Make sure to sanitize strings before passing them to the query to avoid creation of vulnerabilities or unwanted changes to your query.
+
+Expression support is extensible through the Query SPI: `EvaluationContextExtension` & `ReactiveEvaluationContextExtension`
+The Query SPI can contribute properties and functions and can customize the root object.
+Extensions are retrieved from the application context at the time of SpEL evaluation when the query is built.
+The following example shows how to use an evaluation context extension:
+
+[tabs]
+======
+Imperative::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
+----
+public class SampleEvaluationContextExtension extends EvaluationContextExtensionSupport {
+
+ @Override
+ public String getExtensionId() {
+ return "security";
+ }
+
+ @Override
+ public Map getProperties() {
+ return Collections.singletonMap("principal", SecurityContextHolder.getCurrent().getPrincipal());
+ }
+}
+----
+
+Reactive::
++
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+public class SampleEvaluationContextExtension implements ReactiveEvaluationContextExtension {
+
+ @Override
+ public String getExtensionId() {
+ return "security";
+ }
+
+ @Override
+ public Mono extends EvaluationContextExtension> getExtension() {
+ return Mono.just(new EvaluationContextExtensionSupport() { ... });
+ }
+}
+----
+======
+
+NOTE: Bootstrapping `MongoRepositoryFactory` yourself is not application context-aware and requires further configuration to pick up Query SPI extensions.
+
+NOTE: Reactive query methods can make use of `org.springframework.data.spel.spi.ReactiveEvaluationContextExtension`.
+
+[[mongodb.repositories.queries.type-safe]]
+=== Type-safe Query Methods
+
+MongoDB repository and its reactive counterpart integrates with the http://www.querydsl.com/[Querydsl] project, which provides a way to perform type-safe queries.
+
+[quote, Querydsl Team]
+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).
+* Domain types and properties can be referenced safely -- no strings involved!
+* Adapts better to refactoring changes in domain types.
+* Incremental query definition is easier.
+
+See the http://www.querydsl.com/static/querydsl/latest/reference/html/[QueryDSL documentation] for how to bootstrap your environment for APT-based code generation using Maven or Ant.
+
+QueryDSL lets you write queries such as the following:
+
+[tabs]
+======
+Imperative::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
+----
+QPerson person = new QPerson("person");
+List result = repository.findAll(person.address.zipCode.eq("C0123"));
+
+Page page = repository.findAll(person.lastname.contains("a"),
+ PageRequest.of(0, 2, Direction.ASC, "lastname"));
+----
+
+Reactive::
++
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+QPerson person = QPerson.person;
+
+Flux result = repository.findAll(person.address.zipCode.eq("C0123"));
+----
+======
+
+`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` / `ReactiveQuerydslPredicateExecutor` interface, which the following listing shows:
+
+[tabs]
+======
+Imperative::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
+----
+public interface QuerydslPredicateExecutor {
+
+ T findOne(Predicate predicate);
+
+ List findAll(Predicate predicate);
+
+ List findAll(Predicate predicate, Sort sort);
+
+ List findAll(Predicate predicate, OrderSpecifier>... orders);
+
+ Page findAll(Predicate predicate, Pageable pageable);
+
+ List findAll(OrderSpecifier>... orders);
+
+ Long count(Predicate predicate);
+
+ Boolean exists(Predicate predicate);
+}
+----
+
+Reactive::
++
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+interface ReactiveQuerydslPredicateExecutor {
+
+ Mono findOne(Predicate predicate);
+
+ Flux findAll(Predicate predicate);
+
+ Flux findAll(Predicate predicate, Sort sort);
+
+ Flux findAll(Predicate predicate, OrderSpecifier>... orders);
+
+ Flux findAll(OrderSpecifier>... orders);
+
+ Mono count(Predicate predicate);
+
+ Mono exists(Predicate predicate);
+}
+----
+======
+
+To use this in your repository implementation, add it to the list of repository interfaces from which your interface inherits, as the following example shows:
+
+[tabs]
+======
+Imperative::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
+----
+interface PersonRepository extends MongoRepository, QuerydslPredicateExecutor {
+
+ // additional query methods go here
+}
+----
+
+Reactive::
++
+====
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+
+interface PersonRepository extends ReactiveMongoRepository, ReactiveQuerydslPredicateExecutor {
+
+ // additional query methods go here
+}
+----
+
+NOTE: Please note that joins (DBRef's) are not supported with Reactive MongoDB support.
+====
+======
+
+[[mongodb.repositories.queries.full-text]]
+=== Full-text Search Queries
+
+MongoDB's full-text search feature is store-specific and, therefore, can be found on `MongoRepository` rather than on the more general `CrudRepository`.
+We need a document with a full-text index (see "`xref:mongodb/mapping/mapping.adoc#mapping-usage-indexes.text-index[Text Indexes]`" to learn how to create a full-text index).
+
+Additional methods on `MongoRepository` take `TextCriteria` as an input parameter.
+In addition to those explicit methods, it is also possible to add a `TextCriteria`-derived repository method.
+The criteria are added as an additional `AND` criteria.
+Once the entity contains a `@TextScore`-annotated property, the document's full-text score can be retrieved.
+Furthermore, the `@TextScore` annotated also makes it possible to sort by the document's score, as the following example shows:
+
+[source,java]
+----
+@Document
+class FullTextDocument {
+
+ @Id String id;
+ @TextIndexed String title;
+ @TextIndexed String content;
+ @TextScore Float score;
+}
+
+interface FullTextRepository extends Repository {
+
+ // Execute a full-text search and define sorting dynamically
+ List findAllBy(TextCriteria criteria, Sort sort);
+
+ // Paginate over a full-text search result
+ Page findAllBy(TextCriteria criteria, Pageable pageable);
+
+ // Combine a derived query with a full-text search
+ List findByTitleOrderByScoreDesc(String title, TextCriteria criteria);
+}
+
+
+Sort sort = Sort.by("score");
+TextCriteria criteria = TextCriteria.forDefaultLanguage().matchingAny("spring", "data");
+List result = repository.findAllBy(criteria, sort);
+
+criteria = TextCriteria.forDefaultLanguage().matching("film");
+Page page = repository.findAllBy(criteria, PageRequest.of(1, 1, sort));
+List result = repository.findByTitleOrderByScoreDesc("mongodb", criteria);
+----
+
+
+[[mongodb.repositories.queries.update]]
+=== Repository Update Methods
+
+You can also use the keywords in the preceding table to create queries that identify matching documents for running updates on them.
+The actual update action is defined by the `@Update` annotation on the method itself, as the following listing shows.
+Note that the naming schema for derived queries starts with `find`.
+Using `update` (as in `updateAllByLastname(...)`) is allowed only in combination with `@Query`.
+
+The update is applied to *all* matching documents and it is *not* possible to limit the scope by passing in a `Page` or by using any of the <>.
+The return type can be either `void` or a _numeric_ type, such as `long`, to hold the number of modified documents.
+
+.Update Methods
+====
+[source,java]
+----
+public interface PersonRepository extends CrudRepository {
+
+ @Update("{ '$inc' : { 'visits' : 1 } }")
+ long findAndIncrementVisitsByLastname(String lastname); <1>
+
+ @Update("{ '$inc' : { 'visits' : ?1 } }")
+ void findAndIncrementVisitsByLastname(String lastname, int increment); <2>
+
+ @Update("{ '$inc' : { 'visits' : ?#{[1]} } }")
+ long findAndIncrementVisitsUsingSpELByLastname(String lastname, int increment); <3>
+
+ @Update(pipeline = {"{ '$set' : { 'visits' : { '$add' : [ '$visits', ?1 ] } } }"})
+ void findAndIncrementVisitsViaPipelineByLastname(String lastname, int increment); <4>
+
+ @Update("{ '$push' : { 'shippingAddresses' : ?1 } }")
+ long findAndPushShippingAddressByEmail(String email, Address address); <5>
+
+ @Query("{ 'lastname' : ?0 }")
+ @Update("{ '$inc' : { 'visits' : ?1 } }")
+ void updateAllByLastname(String lastname, int increment); <6>
+}
+----
+
+<1> The filter query for the update is derived from the method name.
+The update is "`as is`" and does not bind any parameters.
+<2> The actual increment value is defined by the `increment` method argument that is bound to the `?1` placeholder.
+<3> Use the Spring Expression Language (SpEL) for parameter binding.
+<4> Use the `pipeline` attribute to issue xref:mongodb/template-crud-operations.adoc#mongo-template.aggregation-update[aggregation pipeline updates].
+<5> The update may contain complex objects.
+<6> Combine a xref:mongodb/repositories/repositories.adoc#mongodb.repositories.queries.json-based[string based query] with an update.
+====
+
+WARNING: Repository updates do not emit persistence nor mapping lifecycle events.
+
+[[mongodb.repositories.queries.delete]]
+=== Repository Delete Queries
+
+The keywords in the preceding table can be used in conjunction with `delete…By` or `remove…By` to create queries that delete matching documents.
+
+.`Delete…By` Query
+[tabs]
+======
+Imperative::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
+----
+public interface PersonRepository extends MongoRepository {
+
+ List deleteByLastname(String lastname); <1>
+
+ Long deletePersonByLastname(String lastname); <2>
+
+ @Nullable
+ Person deleteSingleByLastname(String lastname); <3>
+
+ Optional deleteByBirthdate(Date birthdate); <4>
+}
+----
+<1> Using a return type of `List` retrieves and returns all matching documents before actually deleting them.
+<2> A numeric return type directly removes the matching documents, returning the total number of documents removed.
+<3> A single domain type result retrieves and removes the first matching document.
+<4> Same as in 3 but wrapped in an `Optional` type.
+
+Reactive::
++
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+public interface PersonRepository extends ReactiveMongoRepository {
+
+ Flux deleteByLastname(String lastname); <1>
+
+ Mono deletePersonByLastname(String lastname); <2>
+
+ Mono deleteSingleByLastname(String lastname); <3>
+}
+----
+<1> Using a return type of `Flux` retrieves and returns all matching documents before actually deleting them.
+<2> A numeric return type directly removes the matching documents, returning the total number of documents removed.
+<3> A single domain type result retrieves and removes the first matching document.
+======
+
+[[mongodb.repositories.queries.aggregation]]
+== Aggregation Repository Methods
+
+The repository layer offers means to interact with xref:mongodb/aggregation-framework.adoc[the aggregation framework] via annotated repository query methods.
+Similar to the xref:mongodb/repositories/repositories.adoc#mongodb.repositories.queries.json-based[JSON based queries], you can define a pipeline using the `org.springframework.data.mongodb.repository.Aggregation` annotation.
+The definition may contain simple placeholders like `?0` as well as link:{springDocsUrl}/core.html#expressions[SpEL expressions] `?#{ … }`.
+
+.Aggregating Repository Method
+====
+[source,java]
+----
+public interface PersonRepository extends CrudRepository {
+
+ @Aggregation("{ $group: { _id : $lastname, names : { $addToSet : $firstname } } }")
+ List groupByLastnameAndFirstnames(); <1>
+
+ @Aggregation("{ $group: { _id : $lastname, names : { $addToSet : $firstname } } }")
+ List groupByLastnameAndFirstnames(Sort sort); <2>
+
+ @Aggregation("{ $group: { _id : $lastname, names : { $addToSet : ?0 } } }")
+ List groupByLastnameAnd(String property); <3>
+
+ @Aggregation("{ $group: { _id : $lastname, names : { $addToSet : ?0 } } }")
+ Slice groupByLastnameAnd(String property, Pageable page); <4>
+
+ @Aggregation("{ $group: { _id : $lastname, names : { $addToSet : $firstname } } }")
+ Stream groupByLastnameAndFirstnamesAsStream(); <5>
+
+ @Aggregation("{ $group : { _id : null, total : { $sum : $age } } }")
+ SumValue sumAgeUsingValueWrapper(); <6>
+
+ @Aggregation("{ $group : { _id : null, total : { $sum : $age } } }")
+ Long sumAge(); <7>
+
+ @Aggregation("{ $group : { _id : null, total : { $sum : $age } } }")
+ AggregationResults sumAgeRaw(); <8>
+
+ @Aggregation("{ '$project': { '_id' : '$lastname' } }")
+ List findAllLastnames(); <9>
+
+ @Aggregation(pipeline = {
+ "{ $group : { _id : '$author', books: { $push: '$title' } } }",
+ "{ $out : 'authors' }"
+ })
+ void groupAndOutSkippingOutput(); <10>
+}
+----
+[source,java]
+----
+public class PersonAggregate {
+
+ private @Id String lastname; <2>
+ private List names;
+
+ public PersonAggregate(String lastname, List names) {
+ // ...
+ }
+
+ // Getter / Setter omitted
+}
+
+public class SumValue {
+
+ private final Long total; <6> <8>
+
+ public SumValue(Long total) {
+ // ...
+ }
+
+ // Getter omitted
+}
+----
+<1> Aggregation pipeline to group first names by `lastname` in the `Person` collection returning these as `PersonAggregate`.
+<2> If `Sort` argument is present, `$sort` is appended after the declared pipeline stages so that it only affects the order of the final results after having passed all other aggregation stages.
+Therefore, the `Sort` properties are mapped against the methods return type `PersonAggregate` which turns `Sort.by("lastname")` into `{ $sort : { '_id', 1 } }` because `PersonAggregate.lastname` is annotated with `@Id`.
+<3> Replaces `?0` with the given value for `property` for a dynamic aggregation pipeline.
+<4> `$skip`, `$limit` and `$sort` can be passed on via a `Pageable` argument. Same as in <2>, the operators are appended to the pipeline definition. Methods accepting `Pageable` can return `Slice` for easier pagination.
+<5> Aggregation methods can return `Stream` to consume results directly from an underlying cursor. Make sure to close the stream after consuming it to release the server-side cursor by either calling `close()` or through `try-with-resources`.
+<6> Map the result of an aggregation returning a single `Document` to an instance of a desired `SumValue` target type.
+<7> Aggregations resulting in single document holding just an accumulation result like e.g. `$sum` can be extracted directly from the result `Document`.
+To gain more control, you might consider `AggregationResult` as method return type as shown in <7>.
+<8> Obtain the raw `AggregationResults` mapped to the generic target wrapper type `SumValue` or `org.bson.Document`.
+<9> Like in <6>, a single value can be directly obtained from multiple result ``Document``s.
+<10> Skips the output of the `$out` stage when return type is `void`.
+====
+
+In some scenarios, aggregations might require additional options, such as a maximum run time, additional log comments, or the permission to temporarily write data to disk.
+Use the `@Meta` annotation to set those options via `maxExecutionTimeMs`, `comment` or `allowDiskUse`.
+
+[source,java]
+----
+interface PersonRepository extends CrudRepository {
+
+ @Meta(allowDiskUse = true)
+ @Aggregation("{ $group: { _id : $lastname, names : { $addToSet : $firstname } } }")
+ List groupByLastnameAndFirstnames();
+}
+----
+
+Or use `@Meta` to create your own annotation as shown in the sample below.
+
+[source,java]
+----
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ ElementType.METHOD })
+@Meta(allowDiskUse = true)
+@interface AllowDiskUse { }
+
+interface PersonRepository extends CrudRepository {
+
+ @AllowDiskUse
+ @Aggregation("{ $group: { _id : $lastname, names : { $addToSet : $firstname } } }")
+ List groupByLastnameAndFirstnames();
+}
+----
+
+[NOTE]
+====
+Simple-type single-result inspects the returned `Document` and checks for the following:
+
+. Only one entry in the document, return it.
+. Two entries, one is the `_id` value. Return the other.
+. Return for the first value assignable to the return type.
+. Throw an exception if none of the above is applicable.
+====
+
+WARNING: The `Page` return type is not supported for repository methods using `@Aggregation`. However, you can use a
+`Pageable` argument to add `$skip`, `$limit` and `$sort` to the pipeline and let the method return `Slice`.
+
diff --git a/src/main/asciidoc/reference/sharding.adoc b/src/main/antora/modules/ROOT/pages/mongodb/sharding.adoc
similarity index 100%
rename from src/main/asciidoc/reference/sharding.adoc
rename to src/main/antora/modules/ROOT/pages/mongodb/sharding.adoc
diff --git a/src/main/asciidoc/reference/tailable-cursors.adoc b/src/main/antora/modules/ROOT/pages/mongodb/tailable-cursors.adoc
similarity index 95%
rename from src/main/asciidoc/reference/tailable-cursors.adoc
rename to src/main/antora/modules/ROOT/pages/mongodb/tailable-cursors.adoc
index 09a8950f9..97433e141 100644
--- a/src/main/asciidoc/reference/tailable-cursors.adoc
+++ b/src/main/antora/modules/ROOT/pages/mongodb/tailable-cursors.adoc
@@ -1,6 +1,6 @@
// carry over the old bookmarks to prevent external links from failing
[[tailable-cursors]]
-== [[mongo.reactive.repositories.infinite-streams]] Infinite Streams with Tailable Cursors
+= Tailable Cursors
By default, MongoDB automatically closes a cursor when the client exhausts all results supplied by the cursor.
Closing a cursor on exhaustion turns a stream into a finite stream. For https://docs.mongodb.com/manual/core/capped-collections/[capped collections],
@@ -14,7 +14,7 @@ reactive variant, as it is less resource-intensive. However, if you cannot use t
concept that is already prevalent in the Spring ecosystem.
[[tailable-cursors.sync]]
-=== Tailable Cursors with `MessageListener`
+== Tailable Cursors with `MessageListener`
Listening to a capped collection using a Sync Driver creates a long running, blocking task that needs to be delegated to
a separate component. In this case, we need to first create a `MessageListenerContainer`, which will be the main entry point
@@ -50,11 +50,11 @@ container.stop();
<4> Provide an optional filter for documents to receive.
<5> Set the message listener to publish incoming ``Message``s to.
<6> Register the request. The returned `Subscription` can be used to check the current `Task` state and cancel it to free resources.
-<5> Do not forget to stop the container once you are sure you no longer need it. Doing so stops all running `Task` instances within the container.
+<7> Do not forget to stop the container once you are sure you no longer need it. Doing so stops all running `Task` instances within the container.
====
[[tailable-cursors.reactive]]
-=== Reactive Tailable Cursors
+== Reactive Tailable Cursors
Using tailable cursors with a reactive data types allows construction of infinite streams. A tailable cursor remains open until it is closed externally. It emits data as new documents arrive in a capped collection.
diff --git a/src/main/antora/modules/ROOT/pages/mongodb/template-api.adoc b/src/main/antora/modules/ROOT/pages/mongodb/template-api.adoc
new file mode 100644
index 000000000..e25e14e36
--- /dev/null
+++ b/src/main/antora/modules/ROOT/pages/mongodb/template-api.adoc
@@ -0,0 +1,164 @@
+[[mongo-template]]
+= Template API
+
+The `MongoTemplate` and its reactive counterpart class, located in the `org.springframework.data.mongodb.core` package, is the central class of Spring's MongoDB support and provides a rich feature set for interacting with the database.
+The template offers convenience operations to create, update, delete, and query MongoDB documents and provides a mapping between your domain objects and MongoDB documents.
+
+NOTE: Once configured, `MongoTemplate` is thread-safe and can be reused across multiple instances.
+
+[[mongo-template.convenience-methods]]
+== Convenience Methods
+
+The `MongoTemplate` class implements the interface `MongoOperations`.
+In as much as possible, the methods on `MongoOperations` are named after methods available on the MongoDB driver `Collection` object, to make the API familiar to existing MongoDB developers who are used to the driver API.
+For example, you can find methods such as `find`, `findAndModify`, `findAndReplace`, `findOne`, `insert`, `remove`, `save`, `update`, and `updateMulti`.
+The design goal was to make it as easy as possible to transition between the use of the base MongoDB driver and `MongoOperations`.
+A major difference between the two APIs is that `MongoOperations` can be passed domain objects instead of `Document`.
+Also, `MongoOperations` has fluent APIs for `Query`, `Criteria`, and `Update` operations instead of populating a `Document` to specify the parameters for those operations.
+
+For more information please refer to the the xref:mongodb/template-crud-operations.adoc[CRUD] and xref:mongodb/template-query-operations.adoc[Query] sections of the documentation.
+
+NOTE: The preferred way to reference the operations on `MongoTemplate` instance is through its interface, `MongoOperations`.
+
+[[mongo-template.execute-callbacks]]
+== Execute Callbacks
+
+`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 `MongoCollection` or a `MongoDatabase` object.
+
+* ` T` *execute* `(Class> entityClass, CollectionCallback action)`: Runs the given `CollectionCallback` for the entity collection of the specified class.
+
+* ` T` *execute* `(String collectionName, CollectionCallback action)`: Runs the given `CollectionCallback` on the collection of the given name.
+
+* ` T` *execute* `(DbCallback action)`: Runs a DbCallback, translating any exceptions as necessary. Spring Data MongoDB provides support for the Aggregation Framework introduced to MongoDB in version 2.2.
+
+* ` T` *execute* `(String collectionName, DbCallback action)`: Runs a `DbCallback` on the collection of the given name translating any exceptions as necessary.
+
+* ` T` *executeInSession* `(DbCallback action)`: Runs the given `DbCallback` within the same connection to the database so as to ensure consistency in a write-heavy environment where you may read the data that you wrote.
+
+The following example uses the `CollectionCallback` to return information about an index:
+
+[tabs]
+======
+Imperative::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
+----
+boolean hasIndex = template.execute("geolocation", collection ->
+ Streamable.of(collection.listIndexes(org.bson.Document.class))
+ .stream()
+ .map(document -> document.get("name"))
+ .anyMatch("location_2d"::equals)
+);
+----
+
+Reactive::
++
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+Mono hasIndex = template.execute("geolocation", collection ->
+ Flux.from(collection.listIndexes(org.bson.Document.class))
+ .map(document -> document.get("name"))
+ .filterWhen(name -> Mono.just("location_2d".equals(name)))
+ .map(it -> Boolean.TRUE)
+ .single(Boolean.FALSE)
+ ).next();
+----
+======
+
+[[mongo-template.fluent-api]]
+== Fluent API
+
+Being the central component when it comes to more low-level interaction with MongoDB `MongoTemplate` offers a wide range of methods covering needs from collection creation, index creation, and CRUD operations to more advanced functionality, such as Map-Reduce and aggregations.
+You can find multiple overloads for each method.
+Most of them cover optional or nullable parts of the API.
+
+`FluentMongoOperations` provides a more narrow interface for the common methods of `MongoOperations` and provides a more readable, fluent API.
+The entry points (`insert(…)`, `find(…)`, `update(…)`, and others) follow a natural naming schema based on the operation to be run.
+Moving on from the entry point, the API is designed to offer only context-dependent methods that lead to a terminating method that invokes the actual `MongoOperations` counterpart -- the `all` method in the case of the following example:
+
+[tabs]
+======
+Imperative::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
+----
+List all = template.query(SWCharacter.class) <1>
+ .inCollection("star-wars") <2>
+ .as(Jedi.class) <3>
+ .matching(query(where("jedi").is(true))) <4>
+ .all();
+----
+<1> The type used to map fields used in the query to.
+<2> The collection name to use if not defined on the domain type.
+<3> Result type if not using the original domain type.
+<4> The lookup query.
+
+Reactive::
++
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+Flux all = template.query(SWCharacter.class)
+ .inCollection("star-wars")
+ .as(Jedi.class)
+ .matching(query(where("jedi").is(true)))
+ .all();
+----
+======
+
+NOTE: Using projections allows `MongoTemplate` to optimize result mapping by limiting the actual response to fields required
+by the projection target type. This applies as long as the `Query` itself does not contain any field restriction and the
+target type is a closed interface or DTO projection.
+
+WARNING: Projections must not be applied to xref:mongodb/mapping/document-references.adoc[DBRefs].
+
+You can switch between retrieving a single entity and retrieving multiple entities as a `List` or a `Stream` through the terminating methods: `first()`, `one()`, `all()`, or `stream()`.
+
+When writing a geo-spatial query with `near(NearQuery)`, the number of terminating methods is altered to include only the methods that are valid for running a `geoNear` command in MongoDB (fetching entities as a `GeoResult` within `GeoResults`), as the following example shows:
+
+[tabs]
+======
+Imperative::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
+----
+GeoResults results = template.query(SWCharacter.class)
+ .as(Jedi.class)
+ .near(alderaan) // NearQuery.near(-73.9667, 40.78).maxDis…
+ .all();
+----
+
+Reactive::
++
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+Flux> results = template.query(SWCharacter.class)
+ .as(Jedi.class)
+ .near(alderaan) // NearQuery.near(-73.9667, 40.78).maxDis…
+ .all();
+----
+======
+
+[[mongo-template.exception-translation]]
+== Exception Translation
+
+The Spring framework provides exception translation for a wide variety of database and mapping technologies. T
+his has traditionally been for JDBC and JPA.
+The Spring support for MongoDB extends this feature to the MongoDB Database by providing an implementation of the `org.springframework.dao.support.PersistenceExceptionTranslator` interface.
+
+The motivation behind mapping to Spring's link:{springDocsUrl}/data-access.html#dao-exceptions[consistent data access exception hierarchy] is that you are then able to write portable and descriptive exception handling code without resorting to coding against MongoDB error codes.
+All of Spring's data access exceptions are inherited from the root `DataAccessException` class so that you can be sure to catch all database related exception within a single try-catch block.
+Note that not all exceptions thrown by the MongoDB driver inherit from the `MongoException` class.
+The inner exception and message are preserved so that no information is lost.
+
+Some of the mappings performed by the `MongoExceptionTranslator` are `com.mongodb.Network to DataAccessResourceFailureException` and `MongoException` error codes 1003, 12001, 12010, 12011, and 12012 to `InvalidDataAccessApiUsageException`.
+Look into the implementation for more details on the mapping.
+
+[[mongo-template.type-mapping]]
+== Domain Type Mapping
+
+The mapping between MongoDB documents and domain classes is done by delegating to an implementation of the `MongoConverter` interface.
+Spring provides `MappingMongoConverter`, but you can also write your own converter.
+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 xref:mongodb/mapping/mapping.adoc[Mapping] chapter.
diff --git a/src/main/antora/modules/ROOT/pages/mongodb/template-collection-management.adoc b/src/main/antora/modules/ROOT/pages/mongodb/template-collection-management.adoc
new file mode 100644
index 000000000..4e78dc607
--- /dev/null
+++ b/src/main/antora/modules/ROOT/pages/mongodb/template-collection-management.adoc
@@ -0,0 +1,205 @@
+[[mongo-template.index-and-collections]]
+= Index and Collection Management
+
+`MongoTemplate` and `ReactiveMongoTemplate` provide methods for managing indexes and collections.
+These methods are collected into a helper interface called `IndexOperations` respectively `ReactiveIndexOperations`.
+You can access these operations by calling the `indexOps` method and passing in either the collection name or the `java.lang.Class` of your entity (the collection name is derived from the `.class`, either by name or from annotation metadata).
+
+The following listing shows the `IndexOperations` interface:
+
+[tabs]
+======
+Imperative::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
+----
+public interface IndexOperations {
+
+ void ensureIndex(IndexDefinition indexDefinition);
+
+ void alterIndex(String name);
+
+ void dropIndex(String name);
+
+ void dropAllIndexes();
+
+ void resetIndexCache();
+
+ List getIndexInfo();
+}
+----
+
+Reactive::
++
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+public interface ReactiveIndexOperations {
+
+ Mono ensureIndex(IndexDefinition indexDefinition);
+
+ Mono alterIndex(String name, IndexOptions options);
+
+ Mono dropIndex(String name);
+
+ Mono dropAllIndexes();
+
+ Flux getIndexInfo();
+----
+======
+
+[[mongo-template.index-and-collections.index]]
+== Methods for Creating an Index
+
+You can create an index on a collection to improve query performance by using the MongoTemplate class, as the following example shows:
+
+[tabs]
+======
+Imperative::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
+----
+template.indexOps(Person.class)
+ .ensureIndex(new Index().on("name",Order.ASCENDING));
+----
+
+Reactive::
++
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+Mono createIndex = template.indexOps(Person.class)
+ .ensureIndex(new Index().on("name",Order.ASCENDING));
+----
+======
+
+`ensureIndex` makes sure that an index for the provided IndexDefinition exists for the collection.
+
+You can create standard, geospatial, and text indexes by using the `IndexDefinition`, `GeoSpatialIndex` and `TextIndexDefinition` classes.
+For example, given the `Venue` class defined in a previous section, you could declare a geospatial query, as the following example shows:
+
+[source,java]
+----
+template.indexOps(Venue.class)
+ .ensureIndex(new GeospatialIndex("location"));
+----
+
+NOTE: `Index` and `GeospatialIndex` support configuration of xref:mongodb/template-query-options.adoc#mongo.query.collation[collations].
+
+[[mongo-template.index-and-collections.access]]
+== Accessing Index Information
+
+The `IndexOperations` interface has the `getIndexInfo` method that returns a list of `IndexInfo` objects.
+This list contains all the indexes defined on the collection. The following example defines an index on the `Person` class that has an `age` property:
+
+[tabs]
+======
+Imperative::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
+----
+template.indexOps(Person.class)
+ .ensureIndex(new Index().on("age", Order.DESCENDING).unique());
+
+List indexInfoList = template.indexOps(Person.class)
+ .getIndexInfo();
+----
+
+Reactive::
++
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+Mono ageIndex = template.indexOps(Person.class)
+ .ensureIndex(new Index().on("age", Order.DESCENDING).unique());
+
+Flux indexInfo = ageIndex.then(template.indexOps(Person.class)
+ .getIndexInfo());
+----
+======
+
+[[mongo-template.index-and-collections.collection]]
+== Methods for Working with a Collection
+
+The following example shows how to create a collection:
+
+[tabs]
+======
+Imperative::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
+----
+MongoCollection collection = null;
+if (!template.getCollectionNames().contains("MyNewCollection")) {
+ collection = mongoTemplate.createCollection("MyNewCollection");
+}
+----
+
+Reactive::
++
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+MongoCollection collection = template.getCollectionNames().collectList()
+ .flatMap(collectionNames -> {
+ if(!collectionNames.contains("MyNewCollection")) {
+ return template.createCollection("MyNewCollection");
+ }
+ return template.getMongoDatabase().map(db -> db.getCollection("MyNewCollection"));
+ });
+----
+======
+
+NOTE: Collection creation allows customization with `CollectionOptions` and supports xref:mongodb/collation.adoc[collations].
+
+.Methods to interact with MongoCollections
+[%collapsible]
+====
+* *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.
+* *getCollection*: Gets a collection by name, creating it if it does not exist.
+====
+
+[[time-series]]
+== Time Series
+
+MongoDB 5.0 introduced https://docs.mongodb.com/manual/core/timeseries-collections/[Time Series] collections that are optimized to efficiently store documents over time such as measurements or events.
+Those collections need to be created as such before inserting any data.
+Collections can be created by either running the `createCollection` command, defining time series collection options or extracting options from a `@TimeSeries` annotation as shown in the examples below.
+
+.Create a Time Series Collection
+====
+.Create a Time Series via the MongoDB Driver
+[source,java]
+----
+template.execute(db -> {
+
+ com.mongodb.client.model.CreateCollectionOptions options = new CreateCollectionOptions();
+ options.timeSeriesOptions(new TimeSeriesOptions("timestamp"));
+
+ db.createCollection("weather", options);
+ return "OK";
+});
+----
+
+.Create a Time Series Collection with `CollectionOptions`
+[source,java]
+----
+template.createCollection("weather", CollectionOptions.timeSeries("timestamp"));
+----
+
+.Create a Time Series Collection derived from an Annotation
+[source,java]
+----
+@TimeSeries(collection="weather", timeField = "timestamp")
+public class Measurement {
+
+ String id;
+ Instant timestamp;
+ // ...
+}
+
+template.createCollection(Measurement.class);
+----
+====
+
+The snippets above can easily be transferred to the reactive API offering the very same methods.
+Make sure to properly _subscribe_ to the returned publishers.
diff --git a/src/main/asciidoc/reference/mongo-json-schema.adoc b/src/main/antora/modules/ROOT/pages/mongodb/template-collection-schema.adoc
similarity index 94%
rename from src/main/asciidoc/reference/mongo-json-schema.adoc
rename to src/main/antora/modules/ROOT/pages/mongodb/template-collection-schema.adoc
index 928b1fc9b..6c8e20008 100644
--- a/src/main/asciidoc/reference/mongo-json-schema.adoc
+++ b/src/main/antora/modules/ROOT/pages/mongodb/template-collection-schema.adoc
@@ -1,5 +1,5 @@
[[mongo.jsonSchema]]
-=== JSON Schema
+= JSON Schema
As of version 3.6, MongoDB supports collections that validate documents against a provided https://docs.mongodb.com/manual/core/schema-validation/#json-schema[JSON Schema].
The schema itself and both validation action and level can be defined when creating the collection, as the following example shows:
@@ -58,7 +58,7 @@ MongoJsonSchema.builder() <1>
<1> Obtain a schema builder to configure the schema with a fluent API.
<2> Configure required properties either directly as shown here or with more details as in 3.
<3> Configure the required String-typed `firstname` field, allowing only `luke` and `han` values. Properties can be typed or untyped. Use a static import of `JsonSchemaProperty` to make the syntax slightly more compact and to get entry points such as `string(…)`.
-<4> Build the schema object. Use the schema to create either a collection or <>.
+<4> Build the schema object.
====
There are already some predefined and strongly typed schema objects (`JsonSchemaObject` and `JsonSchemaProperty`) available
@@ -87,14 +87,14 @@ template.createCollection(Person.class, CollectionOptions.empty().schema(schema)
====
[[mongo.jsonSchema.generated]]
-==== Generating a Schema
+== Generating a Schema
Setting up a schema can be a time consuming task and we encourage everyone who decides to do so, to really take the time it takes.
It's important, schema changes can be hard.
However, there might be times when one does not want to balked with it, and that is where `JsonSchemaCreator` comes into play.
`JsonSchemaCreator` and its default implementation generates a `MongoJsonSchema` out of domain types metadata provided by the mapping infrastructure.
-This means, that <> as well as potential <> are considered.
+This means, that xref:mongodb/mapping/mapping.adoc#mapping-usage-annotations[annotated properties] as well as potential xref:mongodb/mapping/mapping.adoc#mapping-configuration[custom conversions] are considered.
.Generate Json Schema from domain type
====
@@ -287,23 +287,8 @@ class B extends Root {
----
====
-[[mongo.jsonSchema.query]]
-==== Query a collection for matching JSON Schema
-
-You can use a schema to query any collection for documents that match a given structure defined by a JSON schema, as the following example shows:
-
-.Query for Documents matching a `$jsonSchema`
-====
-[source,java]
-----
-MongoJsonSchema schema = MongoJsonSchema.builder().required("firstname", "lastname").build();
-
-template.find(query(matchingDocumentStructure(schema)), Person.class);
-----
-====
-
[[mongo.jsonSchema.encrypted-fields]]
-==== Encrypted Fields
+== Encrypted Fields
MongoDB 4.2 https://docs.mongodb.com/master/core/security-client-side-encryption/[Field Level Encryption] allows to directly encrypt individual properties.
@@ -399,7 +384,7 @@ public class EncryptionExtension implements EvaluationContextExtension {
====
[[mongo.jsonSchema.types]]
-==== JSON Schema Types
+== JSON Schema Types
The following table shows the supported JSON schema types:
diff --git a/src/main/antora/modules/ROOT/pages/mongodb/template-config.adoc b/src/main/antora/modules/ROOT/pages/mongodb/template-config.adoc
new file mode 100644
index 000000000..25739a8fb
--- /dev/null
+++ b/src/main/antora/modules/ROOT/pages/mongodb/template-config.adoc
@@ -0,0 +1,167 @@
+[[mongo-template.instantiating]]
+= MongoTemplate Configuration
+
+You can use the following configuration to create and register an instance of `MongoTemplate`, as the following example shows:
+
+.Registering a `MongoClient` object and enabling Spring's exception translation support
+[tabs]
+======
+Imperative::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
+----
+@Configuration
+class ApplicationConfiguration {
+
+ @Bean
+ MongoClient mongoClient() {
+ return MongoClients.create("mongodb://localhost:27017");
+ }
+
+ @Bean
+ MongoOperations mongoTemplate(MongoClient mongoClient) {
+ return new MongoTemplate(mongoClient, "geospatial");
+ }
+}
+----
+
+Reactive::
++
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+@Configuration
+class ReactiveApplicationConfiguration {
+
+ @Bean
+ MongoClient mongoClient() {
+ return MongoClients.create("mongodb://localhost:27017");
+ }
+
+ @Bean
+ ReactiveMongoOperations mongoTemplate(MongoClient mongoClient) {
+ return new ReactiveMongoTemplate(mongoClient, "geospatial");
+ }
+}
+----
+
+XML::
++
+[source,xml,indent=0,subs="verbatim,quotes",role="third"]
+----
+
+
+
+
+
+
+----
+======
+
+There are several overloaded constructors of `MongoTemplate` and `ReactiveMongoTemplate`:
+
+* `MongoTemplate(MongoClient mongo, String databaseName)`: Takes the `MongoClient` object and the default database name to operate against.
+* `MongoTemplate(MongoDatabaseFactory mongoDbFactory)`: Takes a MongoDbFactory object that encapsulated the `MongoClient` object, database name, and username and password.
+* `MongoTemplate(MongoDatabaseFactory mongoDbFactory, MongoConverter mongoConverter)`: Adds a `MongoConverter` to use for mapping.
+
+Other optional properties that you might like to set when creating a `MongoTemplate` / `ReactiveMongoTemplate` are the default `WriteResultCheckingPolicy`, `WriteConcern`, `ReadPreference` and others listed below.
+
+[[mongo-template.read-preference]]
+== Default Read Preference
+
+The default read preference applied to read operations if no other preference was defined via the xref:mongodb/template-query-options.adoc#mongo.query.read-preference[Query].
+
+[[mongo-template.writeresultchecking]]
+== WriteResultChecking Policy
+
+When in development, it is handy to either log or throw an exception if the `com.mongodb.WriteResult` returned from any MongoDB operation contains an error. It is quite common to forget to do this during development and then end up with an application that looks like it runs successfully when, in fact, the database was not modified according to your expectations. You can set the `WriteResultChecking` property of `MongoTemplate` to one of the following values: `EXCEPTION` or `NONE`, to either throw an `Exception` or do nothing, respectively. The default is to use a `WriteResultChecking` value of `NONE`.
+
+[[mongo-template.writeconcern]]
+== Default WriteConcern
+
+If it has not yet been specified through the driver at a higher level (such as `com.mongodb.client.MongoClient`), you can set the `com.mongodb.WriteConcern` property that the `MongoTemplate` uses for write operations. If the `WriteConcern` property is not set, it defaults to the one set in the MongoDB driver's DB or Collection setting.
+
+[[mongo-template.writeconcernresolver]]
+== WriteConcernResolver
+
+For more advanced cases where you want to set different `WriteConcern` values on a per-operation basis (for remove, update, insert, and save operations), a strategy interface called `WriteConcernResolver` can be configured on `MongoTemplate`. Since `MongoTemplate` is used to persist POJOs, the `WriteConcernResolver` lets you create a policy that can map a specific POJO class to a `WriteConcern` value. The following listing shows the `WriteConcernResolver` interface:
+
+[source,java]
+----
+public interface WriteConcernResolver {
+ WriteConcern resolve(MongoAction action);
+}
+----
+
+You can use the `MongoAction` argument to determine the `WriteConcern` value or use the value of the Template itself as a default.
+`MongoAction` contains the collection name being written to, the `java.lang.Class` of the POJO, the converted `Document`, the operation (`REMOVE`, `UPDATE`, `INSERT`, `INSERT_LIST`, or `SAVE`), and a few other pieces of contextual information.
+The following example shows two sets of classes getting different `WriteConcern` settings:
+
+[source,java]
+----
+public class MyAppWriteConcernResolver implements WriteConcernResolver {
+
+ @Override
+ public WriteConcern resolve(MongoAction action) {
+ if (action.getEntityType().getSimpleName().contains("Audit")) {
+ return WriteConcern.ACKNOWLEDGED;
+ } else if (action.getEntityType().getSimpleName().contains("Metadata")) {
+ return WriteConcern.JOURNALED;
+ }
+ return action.getDefaultWriteConcern();
+ }
+}
+----
+
+[[mongo-template.entity-lifecycle-events]]
+== Publish entity lifecycle events
+
+The template publishes xref:mongodb/mapping/lifecycle-events.adoc#mongodb.mapping-usage.events[lifecycle events].
+In case there are no listeners present, this feature can be disabled.
+
+[source,java]
+----
+@Bean
+MongoOperations mongoTemplate(MongoClient mongoClient) {
+ MongoTemplate template = new MongoTemplate(mongoClient, "geospatial");
+ template.setEntityLifecycleEventsEnabled(false);
+ // ...
+}
+----
+
+[[mongo-template.entity-callbacks-config]]
+== Configure EntityCallbacks
+
+Nest to lifecycle events the template invokes xref:mongodb/mapping/entity-callbacks.adoc[EntityCallbacks] which can be (if not auto configured) set via the template API.
+
+[tabs]
+======
+Imperative::
++
+[source,java,indent=0,subs="verbatim,quotes",role="primary"]
+----
+@Bean
+MongoOperations mongoTemplate(MongoClient mongoClient) {
+ MongoTemplate template = new MongoTemplate(mongoClient, "...");
+ template.setEntityCallbacks(EntityCallbacks.create(...));
+ // ...
+}
+----
+
+Reactive::
++
+[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
+----
+@Bean
+ReactiveMongoOperations mongoTemplate(MongoClient mongoClient) {
+ ReactiveMongoTemplate template = new ReactiveMongoTemplate(mongoClient, "...");
+ template.setEntityCallbacks(ReactiveEntityCallbacks.create(...));
+ // ...
+}
+----
+======
+
+[[mongo-template.count-documents-config]]
+== Document count configuration
+
+By setting `MongoTemplate#useEstimatedCount(...)` to `true` _MongoTemplate#count(...)_ operations, that use an empty filter query, will be delegated to `estimatedCount`, as long as there is no transaction active and the template is not bound to a xref:mongodb/client-session-transactions.adoc[session].
+Please refer to to the xref:mongodb/template-document-count.adoc#mongo.query.count[Counting Documents] section for more information.
diff --git a/src/main/antora/modules/ROOT/pages/mongodb/template-crud-operations.adoc b/src/main/antora/modules/ROOT/pages/mongodb/template-crud-operations.adoc
new file mode 100644
index 000000000..5366c17ea
--- /dev/null
+++ b/src/main/antora/modules/ROOT/pages/mongodb/template-crud-operations.adoc
@@ -0,0 +1,588 @@
+[[mongo-template.save-update-remove]]
+= Saving, Updating, and Removing Documents
+
+`MongoTemplate` / `ReactiveMongoTemplatge` let you save, update, and delete your domain objects and map those objects to documents stored in MongoDB.
+The API signatures of the imperative and reactive API are mainly the same only differing in their return types.
+While the synchronous API uses `void`, single `Object` and `List` the reactive counterpart consists of `Mono`, `Mono