Skip to content

Commit c86f88c

Browse files
committed
DOCSP-36218: filters with dataclass properties
1 parent 1144bde commit c86f88c

File tree

6 files changed

+76
-5
lines changed

6 files changed

+76
-5
lines changed

examples/src/test/kotlin/DataClassTest.kt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
import com.mongodb.client.model.Filters
3+
import com.mongodb.client.model.Filters.eq
34
import com.mongodb.client.model.FindOneAndUpdateOptions
45
import com.mongodb.client.model.ReturnDocument
56
import com.mongodb.client.model.Updates
@@ -92,10 +93,30 @@ internal class DataClassTest {
9293
.withDocumentClass<NewDataStorage>()
9394
.findOneAndUpdate(filter, update, options)
9495

95-
println("Updated document: ${result}")
96+
println("Updated document: $result")
9697
// :snippet-end:
9798
}
9899

100+
@Test
101+
fun queryDataClassTest() = runBlocking {
102+
103+
val collection = database.getCollection<DataStorage>("data_storage")
104+
105+
// :snippet-start: filters-query-data-class
106+
val record = DataStorage("SSD", 120.0)
107+
// Infixed query
108+
DataStorage::productName.eq(record.productName)
109+
// Nested query
110+
val bson = eq(DataStorage::productName, record.productName)
111+
// Kmongo DSL
112+
val filter = DataStorage::productName eq record.productName
113+
// :snippet-end:
114+
115+
collection.insertOne(record)
116+
val result = collection.find().firstOrNull()
117+
assertEquals(record, result)
118+
}
119+
99120
// :snippet-start: annotated-data-class
100121
data class NetworkDevice(
101122
@BsonId
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
val record = DataStorage("SSD", 120.0)
2+
// Infixed query
3+
DataStorage::productName.eq(record.productName)
4+
// Nested query
5+
val bson = eq(DataStorage::productName, record.productName)
6+
// Kmongo DSL
7+
val filter = DataStorage::productName eq record.productName

source/examples/generated/DataClassTest.snippet.retrieve-diff-data-class.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ val result = collection
1414
.withDocumentClass<NewDataStorage>()
1515
.findOneAndUpdate(filter, update, options)
1616

17-
println("Updated document: ${result}")
17+
println("Updated document: $result")

source/fundamentals/builders/filters.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ with the Kotlin driver:
8181
.. literalinclude:: /examples/generated/FiltersBuildersTest.snippet.paint-order-data-class.kt
8282
:language: kotlin
8383

84+
.. tip:: Filters and Data Class Properties
85+
86+
You can use methods from the ``Filters`` directly with data class
87+
properties. To learn more and view examples, see the
88+
:ref:`kotlin-data-class-query` section of the Document Data Format:
89+
Data Classes guide.
90+
8491
.. _comparison:
8592

8693
Comparison

source/fundamentals/data-formats/document-data-format-data-class.txt

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ Overview
1414
--------
1515

1616
In this guide, you can learn how to store and retrieve data in the
17-
{+driver-long+} using **Kotlin data classes**.
17+
{+driver-long+} by using **{+language+} data classes**.
1818

1919
Serialize and Deserialize a Data Class
2020
--------------------------------------
2121

22-
The driver natively supports encoding and decoding Kotlin data classes for
22+
The driver natively supports encoding and decoding {+language+} data classes for
2323
MongoDB read and write operations using the **default codec registry**. The
2424
default codec registry is a collection of classes called **codecs** that
2525
define how to encode and decode Kotlin and Java types.
@@ -85,6 +85,27 @@ operation adds the ``releaseDate`` field to the document with a
8585
For more information about this feature, see :ref:`Specify Return Type
8686
<db-coll-specify-return-type>` in the Databases and Collections guide.
8787

88+
.. _kotlin-data-class-query:
89+
90+
Querying Data Classes
91+
---------------------
92+
93+
You can use helpers from the ``Filter`` builders class to query on data
94+
class properties. The following code uses the ``Filters.eq()`` method to
95+
construct the same query on the ``DataStorage`` data class in multiple syntaxes:
96+
97+
.. literalinclude:: /examples/generated/DataClassTest.snippet.filters-query-data-class.kt
98+
:language: kotlin
99+
100+
.. tip:: Filters and Data Class Annotations
101+
102+
When you use ``Filters`` class helpers to construct queries on data
103+
classes, the methods respect your data class annotations from the
104+
``bson-kotlin`` and ``bson-kotlinx`` packages. To learn more about
105+
annotations, see the :ref:`fundamentals-data-class-annotations`
106+
section of this guide and the :ref:`kotlin-data-class-annotation`
107+
section in the {+language+} Serialization guide.
108+
88109
.. _fundamentals-data-class-annotations:
89110

90111
Specify Component Conversion Using Annotations
@@ -105,7 +126,7 @@ You can use the following annotations on data classes:
105126
- Description
106127

107128
* - ``BsonId``
108-
- Marks a property to serialize as the _id property.
129+
- Marks a property to serialize as the ``_id`` property.
109130

110131
* - ``BsonProperty``
111132
- Specifies a custom document field name when converting the data class

source/whats-new.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ What's New
1212

1313
Learn what's new in:
1414

15+
* :ref:`Version 5.3 <kotlin-coroutine-version-5.2>`
1516
* :ref:`Version 5.2 <kotlin-coroutine-version-5.2>`
1617
* :ref:`Version 5.1.3 <kotlin-coroutine-version-5.1.3>`
1718
* :ref:`Version 5.1.2 <kotlin-coroutine-version-5.1.2>`
@@ -21,6 +22,20 @@ Learn what's new in:
2122
* :ref:`Version 4.11 <version-4.11>`
2223
* :ref:`Version 4.10 <version-4.10>`
2324

25+
.. _kotlin-coroutine-version-5.3:
26+
27+
What's New in 5.3
28+
-----------------
29+
30+
The 5.2 driver release includes the following new features,
31+
improvements, and fixes:
32+
33+
.. .. sharedinclude:: dbx/jvm/v5.3-wn-items.rst
34+
35+
- Support for using builder class methods directly with data class
36+
properties. To learn more, see the :ref:`fundamentals-data-classes`
37+
guide and the :ref:`kotlin-builders-landing` guides.
38+
2439
.. _kotlin-coroutine-version-5.2:
2540

2641
What's New in 5.2

0 commit comments

Comments
 (0)