Skip to content

DOCSP-36218: builders with dataclass properties #186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this referenced somewhere?

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")
10 changes: 9 additions & 1 deletion source/fundamentals/builders/filters.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ type, which you can pass to any method that expects a query filter.

import com.mongodb.client.model.Filters.*

Most of the ``Filter`` examples in this guide use the following sample collection ``paints``:
Most of the ``Filters`` examples in this guide use the following sample
collection ``paints``:

.. code-block:: json

Expand All @@ -81,6 +82,13 @@ with the Kotlin driver:
.. literalinclude:: /examples/generated/FiltersBuildersTest.snippet.paint-order-data-class.kt
:language: kotlin

.. tip:: Filters and Data Class Properties
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question for technical reviewer:
Should I change the examples on this page instead of adding this note?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The kotlin-extensions module is optional (similar to bson-kotlinx) I think we should keep existing examples and add a section that mentions this module with a couple of examples using data class properties instead of strings.
There are many extensions (see Epic for a general breakdown) I suggest using the tests to pick one or two examples for each category (ex: one example for filter, one for projections, one for updates etc.)

This will be a one stop page/section for people familiar with KMongo or who want to use the driver in a more typesafe manner.


You can use methods from the ``Filters`` class 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,30 @@ 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 ``Filters`` builders class to query on data
class properties. To learn more about this class, see the
:ref:`filters-builders` guide.

The following code uses the ``Filters.eq()`` method to
construct the same query on the ``DataStorage`` data class in multiple syntaxes:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question for technical reviewer:
Is this the correct way to describe this?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in the previous comment, I suggest grouping all these extensions (not just Filters) into a single section. This will improve the discoverability of extensions beyond the common Filter/equality use case.
(see how KMongo goups them here https://litote.org/kmongo/extensions-overview/)

Additionally, we should emphasize that this module is optional (i.e we need to pull implementation("org.mongodb:mongodb-driver-kotlin-extensions:X.Y.Z") dependencyto use it) and clarify the use of different package names. For example, the current string-based gt operator is located in com.mongodb.client.model.Filters, whereas the extensions utilizing data class properties are found in com.mongodb.kotlin.client.model.Filters. This design allows developers to mix both data class property-based and string-based calls within the same application.


.. 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 +129,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
2 changes: 1 addition & 1 deletion source/usage-examples/updateMany.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ bottom of this page.
Example
-------

In this example, we use a ``Filter`` builder to filter our query for
In this example, we use a ``Filters`` builder to filter our query for
movies in the genre "Frequently Discussed".

Next, we update documents that match our query in the ``movies`` collection of the
Expand Down
2 changes: 1 addition & 1 deletion source/usage-examples/updateOne.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ bottom of this page.
Example
-------

In this example, we use a ``Filter`` builder to query the collection for
In this example, we use a ``Filters`` builder to query the collection for
a movie with the title "Cool Runnings 2".

Next, we perform the following updates to the first match for our query
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.3>`
* :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.3 driver release includes the following new features,
improvements, and fixes:

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

- Support for using builders 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
Loading