Skip to content

Commit

Permalink
DOCSP-36218: filters with dataclass properties
Browse files Browse the repository at this point in the history
  • Loading branch information
rustagir committed Jan 7, 2025
1 parent 1144bde commit c86f88c
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 5 deletions.
23 changes: 22 additions & 1 deletion examples/src/test/kotlin/DataClassTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import com.mongodb.client.model.Filters
import com.mongodb.client.model.Filters.eq
import com.mongodb.client.model.FindOneAndUpdateOptions
import com.mongodb.client.model.ReturnDocument
import com.mongodb.client.model.Updates
Expand Down Expand Up @@ -92,10 +93,30 @@ internal class DataClassTest {
.withDocumentClass<NewDataStorage>()
.findOneAndUpdate(filter, update, options)

println("Updated document: ${result}")
println("Updated document: $result")
// :snippet-end:
}

@Test
fun queryDataClassTest() = runBlocking {

val collection = database.getCollection<DataStorage>("data_storage")

// :snippet-start: filters-query-data-class
val record = DataStorage("SSD", 120.0)
// Infixed query
DataStorage::productName.eq(record.productName)
// Nested query
val bson = eq(DataStorage::productName, record.productName)
// Kmongo DSL
val filter = DataStorage::productName eq record.productName
// :snippet-end:

collection.insertOne(record)
val result = collection.find().firstOrNull()
assertEquals(record, result)
}

// :snippet-start: annotated-data-class
data class NetworkDevice(
@BsonId
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
val record = DataStorage("SSD", 120.0)
// Infixed query
DataStorage::productName.eq(record.productName)
// Nested query
val bson = eq(DataStorage::productName, record.productName)
// Kmongo DSL
val filter = DataStorage::productName eq record.productName
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ val result = collection
.withDocumentClass<NewDataStorage>()
.findOneAndUpdate(filter, update, options)

println("Updated document: ${result}")
println("Updated document: $result")
7 changes: 7 additions & 0 deletions source/fundamentals/builders/filters.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ with the Kotlin driver:
.. literalinclude:: /examples/generated/FiltersBuildersTest.snippet.paint-order-data-class.kt
:language: kotlin

.. tip:: Filters and Data Class Properties

You can use methods from the ``Filters`` directly with data class
properties. To learn more and view examples, see the
:ref:`kotlin-data-class-query` section of the Document Data Format:
Data Classes guide.

.. _comparison:

Comparison
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ Overview
--------

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

Serialize and Deserialize a Data Class
--------------------------------------

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

.. _kotlin-data-class-query:

Querying Data Classes
---------------------

You can use helpers from the ``Filter`` builders class to query on data
class properties. The following code uses the ``Filters.eq()`` method to
construct the same query on the ``DataStorage`` data class in multiple syntaxes:

.. literalinclude:: /examples/generated/DataClassTest.snippet.filters-query-data-class.kt
:language: kotlin

.. tip:: Filters and Data Class Annotations

When you use ``Filters`` class helpers to construct queries on data
classes, the methods respect your data class annotations from the
``bson-kotlin`` and ``bson-kotlinx`` packages. To learn more about
annotations, see the :ref:`fundamentals-data-class-annotations`
section of this guide and the :ref:`kotlin-data-class-annotation`
section in the {+language+} Serialization guide.

.. _fundamentals-data-class-annotations:

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

* - ``BsonId``
- Marks a property to serialize as the _id property.
- Marks a property to serialize as the ``_id`` property.

* - ``BsonProperty``
- Specifies a custom document field name when converting the data class
Expand Down
15 changes: 15 additions & 0 deletions source/whats-new.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ What's New

Learn what's new in:

* :ref:`Version 5.3 <kotlin-coroutine-version-5.2>`
* :ref:`Version 5.2 <kotlin-coroutine-version-5.2>`
* :ref:`Version 5.1.3 <kotlin-coroutine-version-5.1.3>`
* :ref:`Version 5.1.2 <kotlin-coroutine-version-5.1.2>`
Expand All @@ -21,6 +22,20 @@ Learn what's new in:
* :ref:`Version 4.11 <version-4.11>`
* :ref:`Version 4.10 <version-4.10>`

.. _kotlin-coroutine-version-5.3:

What's New in 5.3
-----------------

The 5.2 driver release includes the following new features,
improvements, and fixes:

.. .. sharedinclude:: dbx/jvm/v5.3-wn-items.rst

- Support for using builder class methods directly with data class
properties. To learn more, see the :ref:`fundamentals-data-classes`
guide and the :ref:`kotlin-builders-landing` guides.

.. _kotlin-coroutine-version-5.2:

What's New in 5.2
Expand Down

0 comments on commit c86f88c

Please sign in to comment.