DATAMONGO-2138 - Polishing.
Rename NestedProperty to KPropertyPath to reflect the underlying concept in alignment with our own PropertyPath type. Rename nestedFieldName(…) method to asString(…) to align with Kotlin method terminology. Reformat. Slightly reword documentation. Add Type-safe Queries for Kotlin to What's New section. Original pull request: #622.
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
[[new-features]]
|
||||
= New & Noteworthy
|
||||
|
||||
[[new-features.2-2-0]]
|
||||
== What's New in Spring Data MongoDB 2.2
|
||||
* <<mongo.query.kotlin-support,Type-safe Queries for Kotlin>>
|
||||
|
||||
[[new-features.2-1-0]]
|
||||
== What's New in Spring Data MongoDB 2.1
|
||||
|
||||
|
||||
@@ -1795,10 +1795,12 @@ GeoResults<Jedi> results = mongoOps.query(SWCharacter.class)
|
||||
[[mongo.query.kotlin-support]]
|
||||
=== Type-safe Queries for Kotlin
|
||||
|
||||
https://kotlinlang.org/docs/reference/reflection.html#property-references[Kotlin property references]
|
||||
can be used to build type-safe queries.
|
||||
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`.
|
||||
|
||||
Most methods in `Criteria` have a matching Kotlin extension, like `inValues` and `regex`.
|
||||
Consider the following example explaining Type-safe Queries:
|
||||
|
||||
====
|
||||
[source,kotlin]
|
||||
@@ -1806,21 +1808,29 @@ Most methods in `Criteria` have a matching Kotlin extension, like `inValues` and
|
||||
import org.springframework.data.mongodb.core.query.*
|
||||
|
||||
mongoOperations.find<Book>(
|
||||
Query(Book::title isEqualTo "Moby-Dick") <1>
|
||||
Query(Book::title isEqualTo "Moby-Dick") <1>
|
||||
)
|
||||
|
||||
Book::title exists true
|
||||
mongoOperations.find<Book>(
|
||||
Query(titlePredicate = Book::title exists true)
|
||||
)
|
||||
|
||||
Criteria().andOperator(
|
||||
Book::price gt 5,
|
||||
Book::price lt 10
|
||||
mongoOperations.find<Book>(
|
||||
Criteria().andOperator(
|
||||
Book::price gt 5,
|
||||
Book::price lt 10
|
||||
)
|
||||
)
|
||||
|
||||
// Binary operators
|
||||
BinaryMessage::payload bits { allClear(0b101) } <2>
|
||||
mongoOperations.find<BinaryMessage>(
|
||||
Query(BinaryMessage::payload bits { allClear(0b101) }) <2>
|
||||
)
|
||||
|
||||
// Nested Properties (i.e. refer to "book.author")
|
||||
Book::author / Author::name regex "^H" <3>
|
||||
mongoOperations.find<Book>(
|
||||
Query(Book::author / Author::name regex "^H") <3>
|
||||
)
|
||||
----
|
||||
<1> `isEqualTo()` is an infix extension function with receiver type `KProperty<T>` that returns `Criteria`.
|
||||
<2> For bitwise operators, pass a lambda argument where you call one of the methods of `Criteria.BitwiseCriteriaOperators`.
|
||||
|
||||
Reference in New Issue
Block a user