Skip to content

Commit 6907000

Browse files
committed
DATAMONGO-2138 - Document Typed Queries
1 parent 447ccb2 commit 6907000

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/main/asciidoc/reference/mongodb.adoc

+32
Original file line numberDiff line numberDiff line change
@@ -1821,6 +1821,38 @@ List<Person> findByFirstname(String firstname);
18211821
----
18221822
====
18231823

1824+
[[mongo.query.kotlin-support]]
1825+
=== Typed Queries for Kotlin
1826+
1827+
https://kotlinlang.org/docs/reference/reflection.html#property-references[Kotlin property references]
1828+
can be used to build typed queries.
1829+
1830+
Most methods in `Criteria` have a matching Kotlin extension, like `inValues` and `regex`.
1831+
1832+
====
1833+
[source,kotlin]
1834+
----
1835+
import org.springframework.data.mongodb.core.query.*
1836+
1837+
mongoOperations.find<Book>(
1838+
Query(Book::name isEqualTo "Moby-Dick") <1>
1839+
)
1840+
1841+
// vararg & infix variations
1842+
Book::category.inValues("Fantasy", "Science fiction")
1843+
Book::category inValues listOf("Fantasy", "Science fiction")
1844+
1845+
// Binary operators
1846+
BinaryMessage::payload bits { allClear(0b101) } <2>
1847+
1848+
// Nested Properties (i.e. refer to "book.author")
1849+
Book::author / Author::name regex "^H" <3>
1850+
----
1851+
<1> `isEqualTo()` is an infix extension function with receiver type `KProperty<T>` that returns `Criteria`.
1852+
<2> For bitwise operators, pass a lambda argument where you call one of the methods of `Criteria.BitwiseCriteriaOperators`.
1853+
<3> To construct nested properties, use the `/` character (overloaded operator `div`).
1854+
====
1855+
18241856
include::../{spring-data-commons-docs}/query-by-example.adoc[leveloffset=+1]
18251857
include::query-by-example.adoc[leveloffset=+1]
18261858

0 commit comments

Comments
 (0)