Skip to content

DOCSP-46772: sort option for client bw - updateone & replaceone #85

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 2 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 22 additions & 2 deletions source/includes/write/client-bulk-write.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import com.mongodb.MongoNamespace
import com.mongodb.client.model.Filters
import com.mongodb.client.model.Filters.eq
import com.mongodb.client.model.Updates
import com.mongodb.client.model.bulk.ClientBulkWriteOptions
import com.mongodb.client.model.bulk.ClientBulkWriteResult
import com.mongodb.client.model.bulk.ClientNamespacedWriteModel
Expand All @@ -12,11 +14,13 @@ data class Restaurant(
val name: String,
val borough: String,
val cuisine: String,
val stars: Int? = null,
)

data class Movie(
val title: String,
val year: Int
val year: Int,
val seen: Boolean? = null,
)
// end-data-classes

Expand All @@ -39,6 +43,22 @@ fun main() {
)
// end-insert-models

// start-update-models
val restaurantUpdate = ClientNamespacedWriteModel
.updateOne(
MongoNamespace("sample_restaurants", "restaurants"),
Filters.eq(Restaurant::name.name, "Villa Berulia"),
Updates.inc(Restaurant::stars.name, 1)
)

val movieUpdate = ClientNamespacedWriteModel
.updateMany(
MongoNamespace("sample_mflix", "movies"),
Filters.eq(Movie::title.name, "Carrie"),
Updates.set(Movie::seen.name, true)
)
// end-update-models

// start-replace-models
val restaurantReplacement = ClientNamespacedWriteModel
.replaceOne(
Expand Down Expand Up @@ -102,7 +122,7 @@ fun main() {
)
)

val result= mongoClient
val result = mongoClient
.bulkWrite(bulkOps, options)
// end-options
}
Expand Down
17 changes: 17 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.4 <kotlin-sync-version-5.4>`
* :ref:`Version 5.3 <kotlin-sync-version-5.3>`
* :ref:`Version 5.2 <kotlin-sync-version-5.2>`
* :ref:`Version 5.1.3 <kotlin-sync-version-5.1.3>`
Expand All @@ -20,6 +21,22 @@ Learn what's new in:
* :ref:`Version 5.1 <kotlin-sync-version-5.1>`
* :ref:`Version 5.0 <kotlin-sync-version-5.0>`

.. _kotlin-sync-version-5.4:

What's New in 5.4
-----------------

The 5.4 driver release includes the following changes, fixes,
and features:

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

.. replacement:: sort-option-link

the :ref:`kotlin-sync-client-bulk-write-update` and
:ref:`kotlin-sync-client-bulk-write-replace` sections of the Bulk Write
Operations guide

.. _kotlin-sync-version-5.3:

What's New in 5.3
Expand Down
69 changes: 60 additions & 9 deletions source/write/bulk-write.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Atlas </getting-started>` guide.
The documents in these collections are modeled by the following
{+language+} data classes:

.. literalinclude:: /includes/write/bulk.kt
.. literalinclude:: /includes/write/client-bulk-write.kt
:start-after: start-data-classes
:end-before: end-data-classes
:language: kotlin
Expand Down Expand Up @@ -138,7 +138,7 @@ The following example creates an instance of ``UpdateOneModel``:
If multiple documents match the query filter specified in
the ``UpdateOneModel`` instance, the operation updates the first
result. You can specify a sort in an ``UpdateOptions`` instance to apply
an order to matched documents before the driver performs the update
an order to matched documents before the server performs the update
operation, as shown in the following code:

.. code-block:: kotlin
Expand Down Expand Up @@ -181,7 +181,7 @@ The following example creates an instance of ``ReplaceOneModel``:
If multiple documents match the query filter specified in
the ``ReplaceOneModel`` instance, the operation replaces the first
result. You can specify a sort in a ``ReplaceOptions`` instance to apply
an order to matched documents before the driver performs the replace
an order to matched documents before the server performs the replace
operation, as shown in the following code:

.. code-block:: kotlin
Expand Down Expand Up @@ -467,8 +467,8 @@ Insert Operations
~~~~~~~~~~~~~~~~~

This example shows how to create models that contain instructions to
insert two documents. One document is inserted into the ``db.people``
collection, and the other document is inserted into the ``db.things`` collection.
insert two documents. One document is inserted into the ``sample_restaurants.restaurants``
collection, and the other document is inserted into the ``sample_mflix.movies`` collection.
The ``MongoNamespace`` instance defines the target database and collection that
each write operation applies to.

Expand All @@ -479,11 +479,49 @@ each write operation applies to.
:copyable:
:dedent:

.. _kotlin-sync-client-bulk-write-update:

Update Operation
~~~~~~~~~~~~~~~~

The following example shows how to use the ``bulkWrite()`` method to update
existing documents in the ``sample_restaurants.restaurants`` and
``sample_mflix.movies`` collections:

.. literalinclude:: /includes/write/client-bulk-write.kt
:start-after: start-update-models
:end-before: end-update-models
:language: kotlin
:copyable:
:dedent:

This example increments the value of the ``stars`` field by ``1`` in the
document that has a ``name`` value of ``"Villa Berulia"`` in the
``restaurants`` collection. It also sets the value of the ``seen`` field
to ``true`` in all documents that have a ``title`` value of ``"Carrie"``
in the ``movies`` collection.

If multiple documents match the query filter specified in
a ``ClientNamespacedUpdateOneModel`` instance, the operation updates the
first result. You can specify a sort order in a `ClientUpdateOneOptions
<{+core-api+}/com/mongodb/client/model/bulk/ClientUpdateOneOptions.html>`__
instance to apply an order to matched documents before the server
performs the update operation, as shown in the following code:

.. code-block:: kotlin

val options = ClientUpdateOneOptions
.clientUpdateOneOptions()
.sort(Sorts.ascending("_id"))

.. _kotlin-sync-client-bulk-write-replace:

Replace Operations
~~~~~~~~~~~~~~~~~~

The following example shows how to create models to replace
existing documents in the ``db.people`` and ``db.things`` collections:
existing documents in the ``sample_restaurants.restaurants`` and
``sample_mflix.movies`` collections:

.. literalinclude:: /includes/write/client-bulk-write.kt
:start-after: start-replace-models
Expand All @@ -493,9 +531,22 @@ existing documents in the ``db.people`` and ``db.things`` collections:
:dedent:

After this example runs successfully, the document that has an ``_id`` value of ``1``
in the ``people`` collection is replaced with a new document. The document in
the ``things`` collection that has an ``_id`` value of ``1``
is replaced with a new document.
in the ``restaurants`` collection is replaced with a new document. The document in
the ``movies`` collection that has an ``_id`` value of ``1``
is also replaced with a new document.

If multiple documents match the query filter specified in
a ``ClientNamespacedReplaceOneModel`` instance, the operation replaces the
first result. You can specify a sort order in a `ClientReplaceOneOptions
<{+core-api+}/com/mongodb/client/model/bulk/ClientReplaceOneOptions.html>`__
instance to apply an order to matched documents before the driver
performs the replace operation, as shown in the following code:

.. code-block:: kotlin

val options = ClientReplaceOneOptions
.clientReplaceOneOptions()
.sort(Sorts.ascending("_id"))

Perform the Bulk Operation
~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
Loading