Skip to content

DOCSP-41448 Write Update Operations Technical Review #48

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
10 changes: 5 additions & 5 deletions source/includes/reactor-note.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.. important:: Project Reactor Library

This guide uses the Project Reactor library to implement ``Publisher`` and
``Subscriber`` instances. To learn more about the Project Reactor library and how
to use Reactor, see `Getting Started
<https://projectreactor.io/docs/core/release/reference/#getting-started>`__
in the Reactor documentation.
This guide uses the Project Reactor library to consume ``Publishers`` returned
by the {+driver-short+} methods. To learn more about the Project Reactor library
and how to use it, see `Getting Started <https://projectreactor.io/docs/core/release/reference/#getting-started>`__
in the Reactor documentation. To learn more about how we use Project Reactor
library methods in this guide, see :ref:`Write Data to MongoDB <write-data-reactor-overview>`.
102 changes: 44 additions & 58 deletions source/tutorials/write-update-documents.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ Update Documents
:values: reference

.. meta::
:keywords: code examples, write, change, edit
:keywords: code examples, write, change, edit, new

Overview
--------

In this guide, you can learn how to use the {+driver-short+} to delete
documents from a MongoDB collection by performing update operations.
In this guide, you can learn how to use the {+driver-short+} to update
documents in a MongoDB collection by performing update operations.

An update operation updates one or more documents in a MongoDB collection.
You can perform an update operation by using the ``updateOne()`` or
Expand All @@ -33,17 +33,10 @@ Sample Data
The examples in this guide use the ``restaurants`` collection
from the ``sample_restaurants`` database in the :atlas:`Atlas sample datasets </sample-data>`.

.. TODO:
.. Once get started ticket merged in, link the get started tutorial below

.. To learn how to create a
.. free MongoDB Atlas cluster and load the sample datasets, see the
.. :ref:`<java-rs-getting-started>` tutorial.

.. TODO: When write insert methods merged in, add subscriber methods section includes within subscriber methods section
To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the
:ref:`<java-rs-getting-started>` tutorial.

.. Subscriber Methods
.. ~~~~~~~~~~~~~~~~~~~
.. include:: /includes/reactor-note.rst

Update Operations
-----------------
Expand All @@ -55,11 +48,11 @@ You can perform update operations in MongoDB by using the following methods:

Each update method requires the following parameters:

- A query filter document, which determines the documents to update. For more
- **Query filter** document, which determines the documents to update. For more
information about using query filters, see the :ref:`Filters
<java-rs-update-filters>` section.

- An update document, which specifies the update operator (the kind of update to
- **Update** document, which specifies the update operator (the kind of update to
perform) and the fields and values to change. For more information
about update operators, see the :ref:`Update Operators
<java-rs-update-operators>` section.
Expand Down Expand Up @@ -101,42 +94,50 @@ To learn more about update operators, see
Update One Document
-------------------

To update a single document from a MongoDB collection, call the ``updateOne()``
method and pass your query filter as the first argument. Then, pass your
update operators as the second argument to the ``updateOne()`` method. After you call the
``updateOne()`` method, chain the ``subscribe()`` method and pass a subscriber
as an argument.

The following example uses ``Updates.set()`` to update the ``name``
To update a single document in a MongoDB collection, call the ``updateOne()``
method and pass your query filter and update operators. Then, pass the
``updateOne()`` result to the static ``Mono.from()`` method from
``Mono``. ``Mono`` is a class from the Project Reactor library. In {+java-rs+},
the driver methods return cold ``Publishers``, which means that the
corresponding operation does not happen unless you subscribe to the returned
``Publisher``. This guide uses the Project Reactor library to consume them. To learn more
about ``Mono``, see `Mono <https://projectreactor.io/docs/core/release/reference/#mono>`__ in the Project
Reactor documentation.

The following example uses the ``updateOne()`` method to update the ``name``
value of a matching document from ``"Bagels N Buns"`` to ``"2 Bagels 2 Buns"``:

.. code-block:: java
:copyable: true

ObservableSubscriber<UpdateResult> updateOneSubscriber = new SubscriberHelpers.OperationSubscriber<>();
restaurants.updateOne(eq("name", "Bagels N Buns"), set("name", "2
Bagels 2 Buns")).subscribe(updateOneSubscriber);
updateOneSubscriber.await();
Publisher<UpdateResult> updatePublisher =
restaurants.updateOne(eq("name", "Bagels N Buns"),
set("name", "2 Bagels 2 Buns"));
Mono.from(updatePublisher).block();

Update Multiple Documents
-------------------------

To update multiple documents from a MongoDB collection, call the ``updateMany()``
method and pass your query filter. Then, pass your
update operators as the second argument to the ``updateMany()`` method. After you call the
``updateMany()`` method, chain the ``subscribe()`` method and pass a subscriber
as an argument.
To update multiple documents in a MongoDB collection, call the ``updateMany()``
method and pass your query filter and update operators. Then, pass the ``updateMany()`` result to the static ``Mono.from()`` method from
``Mono``. ``Mono`` is a class from the Project Reactor library. In {+java-rs+},
the driver methods return cold ``Publishers``, which means that the
corresponding operation does not happen unless you subscribe to the returned
``Publisher``. This guide uses the Project Reactor library to consume them. To learn more
about ``Mono``, see `Mono <https://projectreactor.io/docs/core/release/reference/#mono>`__ in the Project
Reactor documentation.

The following example updates all documents that have a ``cuisine`` value of
The following example uses the ``updateMany()`` method to update all documents that have a ``cuisine`` value of
``"Pizza"`` to have a ``cuisine`` value of ``"Pasta"``:

.. code-block:: java
:copyable: true

ObservableSubscriber<UpdateResult> updateManySubscriber = new SubscriberHelpers.OperationSubscriber<>();
restaurants.updateMany(eq("cuisine", "Pizza"), set("cuisine", "Pasta")).subscribe(updateManySubscriber);
updateManySubscriber.await();

Publisher<UpdateResult> updatePublisher =
restaurants.updateMany(eq("cuisine", "Pizza"),
set("cuisine", "Pasta"));
Mono.from(updatePublisher).block();

Customize the Update Operation
------------------------------

Expand Down Expand Up @@ -216,14 +217,15 @@ value in these documents to ``"Manhattan (north)"``. Because the ``upsert`` opti
set to ``true``, the {+driver-short+} inserts a new document if the query filter doesn't
match any existing documents.


.. code-block:: java
:copyable: true

ObservableSubscriber <UpdateResult> updateManySubscriber = new SubscriberHelpers.OperationSubscriber<>();
restaurants.updateMany(eq("borough", "Manhattan"), set("borough", "Manhattan
(north)"), new UpdateOptions().upsert(true)).subscribe(updateManySubscriber);
updateManySubscriber.await();
Publisher<UpdateResult> updatePublisher = restaurants.updateMany(
eq("borough", "Manhattan"),
set("borough", "Manhattan (north)"),
new UpdateOptions().upsert(true));
Mono.from(updatePublisher).block();


Return Value
------------
Expand Down Expand Up @@ -279,20 +281,4 @@ guide, see the following API documentation:
- `BsonValue
<https://mongodb.github.io/mongo-java-driver/5.1/apidocs/bson/org/bson/BsonValue.html>`__
- `Collation
<https://mongodb.github.io/mongo-java-driver/5.1/apidocs/mongodb-driver-core/com/mongodb/client/model/Collation.html>`__
















<https://mongodb.github.io/mongo-java-driver/5.1/apidocs/mongodb-driver-core/com/mongodb/client/model/Collation.html>`__
2 changes: 2 additions & 0 deletions source/write-data-to-mongo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Write Data to MongoDB
Overview
--------

.. _write-data-reactor-overview:

This page contains copyable code examples of {+driver-short+} methods that you can use to
write data to MongoDB.

Expand Down