|
| 1 | +.. _kotlin-sync-write-update: |
| 2 | + |
| 3 | +================ |
| 4 | +Update Documents |
| 5 | +================ |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :keywords: modify, change, operator, code example |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to use the {+driver-short+} to update |
| 24 | +documents in a MongoDB collection by using the ``updateOne()`` and |
| 25 | +``updateMany()`` methods. |
| 26 | + |
| 27 | +Sample Data |
| 28 | +~~~~~~~~~~~ |
| 29 | + |
| 30 | +The examples in this guide use the ``sample_restaurants.restaurants`` collection |
| 31 | +from the :atlas:`Atlas sample datasets </sample-data>`. To learn how to create a |
| 32 | +free MongoDB Atlas cluster and load the sample datasets, see the |
| 33 | +:atlas:`Get Started with Atlas </getting-started>` guide. |
| 34 | + |
| 35 | +The documents in this collection are modeled by the following {+language+} data class: |
| 36 | + |
| 37 | +.. literalinclude:: /includes/write/update.kt |
| 38 | + :start-after: start-data-class |
| 39 | + :end-before: end-data-class |
| 40 | + :language: kotlin |
| 41 | + :copyable: |
| 42 | + :dedent: |
| 43 | + |
| 44 | +Update Operations |
| 45 | +----------------- |
| 46 | + |
| 47 | +You can update documents in MongoDB by using the following methods: |
| 48 | + |
| 49 | +- ``updateOne()``, which updates *the first document* that matches the search criteria |
| 50 | +- ``updateMany()``, which updates *all documents* that match the search criteria |
| 51 | + |
| 52 | +Each update method requires the following parameters: |
| 53 | + |
| 54 | +- **Query filter**, which matches which documents to update. To learn |
| 55 | + more about query filters, see the :ref:`kotlin-sync-specify-query` |
| 56 | + guide. |
| 57 | + |
| 58 | +- **Update document**, which specifies the update operator, or the kind of update to |
| 59 | + perform, and the fields and values to be updated. For a list of update |
| 60 | + operators and their usages, see the :manual:`Field Update Operators |
| 61 | + guide page</reference/operator/update-field/>` in the {+mdb-server+} manual. |
| 62 | + |
| 63 | +Update One Document |
| 64 | +~~~~~~~~~~~~~~~~~~~ |
| 65 | + |
| 66 | +The following example uses the ``updateOne()`` method to update the |
| 67 | +``name`` value of a document from ``"Happy Garden"`` to ``"Mountain |
| 68 | +House"``: |
| 69 | + |
| 70 | +.. literalinclude:: /includes/write/update.kt |
| 71 | + :start-after: start-update-one |
| 72 | + :end-before: end-update-one |
| 73 | + :language: kotlin |
| 74 | + :copyable: |
| 75 | + :dedent: |
| 76 | + |
| 77 | +Update Many Documents |
| 78 | +~~~~~~~~~~~~~~~~~~~~~ |
| 79 | + |
| 80 | +The following example uses the ``updateMany()`` method to update all documents |
| 81 | +in which the ``name`` value is ``"Starbucks"``. The update renames the |
| 82 | +``address`` field to ``location``. |
| 83 | + |
| 84 | +.. literalinclude:: /includes/write/update.kt |
| 85 | + :start-after: start-update-many |
| 86 | + :end-before: end-update-many |
| 87 | + :language: kotlin |
| 88 | + :copyable: |
| 89 | + :dedent: |
| 90 | + |
| 91 | +Customize the Update Operation |
| 92 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 93 | + |
| 94 | +The ``updateOne()`` and ``updateMany()`` methods optionally accept |
| 95 | +a parameter that sets options to configure the update operation. |
| 96 | +If you don't specify any options, the driver performs update |
| 97 | +operations with default settings. |
| 98 | + |
| 99 | +The following table describes the setter methods that you can use to |
| 100 | +configure an ``UpdateOptions`` instance: |
| 101 | + |
| 102 | +.. list-table:: |
| 103 | + :widths: 30 70 |
| 104 | + :header-rows: 1 |
| 105 | + |
| 106 | + * - Property |
| 107 | + - Description |
| 108 | + |
| 109 | + * - ``upsert()`` |
| 110 | + - | Specifies whether the update operation performs an upsert operation if no |
| 111 | + documents match the query filter. For more information, see the :manual:`upsert |
| 112 | + statement </reference/command/update/#std-label-update-command-upsert>` |
| 113 | + in the {+mdb-server+} manual. |
| 114 | + | Defaults to ``false`` |
| 115 | + |
| 116 | + * - ``bypassDocumentValidation()`` |
| 117 | + - | Specifies whether the update operation bypasses document validation. This lets you |
| 118 | + update documents that don't meet the schema validation requirements, if any |
| 119 | + exist. For more information about schema validation, see :manual:`Schema |
| 120 | + Validation </core/schema-validation/#schema-validation>` in the MongoDB |
| 121 | + Server manual. |
| 122 | + | Defaults to ``false``. |
| 123 | + |
| 124 | + * - ``collation()`` |
| 125 | + - | Specifies the kind of language collation to use when sorting |
| 126 | + results. For more information, see :manual:`Collation </reference/collation/#std-label-collation>` |
| 127 | + in the {+mdb-server+} manual. |
| 128 | + |
| 129 | + * - ``arrayFilters()`` |
| 130 | + - | Provides a list of filters that you specify to select which |
| 131 | + array elements the update applies to. |
| 132 | + |
| 133 | + * - ``hint()`` |
| 134 | + - | Sets the index to use when matching documents. |
| 135 | + For more information, see the :manual:`hint statement </reference/command/update/#std-label-update-command-hint>` |
| 136 | + in the {+mdb-server+} manual. |
| 137 | + |
| 138 | + * - ``let()`` |
| 139 | + - | Provides a map of parameter names and values to set top-level |
| 140 | + variables for the operation. Values must be constant or closed |
| 141 | + expressions that don't reference document fields. For more information, |
| 142 | + see the :manual:`let statement |
| 143 | + </reference/command/update/#std-label-update-let-syntax>` in the |
| 144 | + {+mdb-server+} manual. |
| 145 | + |
| 146 | + * - ``comment()`` |
| 147 | + - | Sets a comment to attach to the operation. For more |
| 148 | + information, see the :manual:`update command |
| 149 | + fields </reference/command/update/#update>` guide in the |
| 150 | + {+mdb-server+} manual for more information. |
| 151 | + |
| 152 | +Modify Update Example |
| 153 | +````````````````````` |
| 154 | + |
| 155 | +The following code uses the ``updateOne()`` method to match documents |
| 156 | +in which the ``name`` field value is ``"Sunrise Pizzeria"``. It then |
| 157 | +sets the ``borough`` value in the first matching document to |
| 158 | +``"Queens"`` and the ``cuisine`` value to ``"Italian"``. |
| 159 | + |
| 160 | +Because the ``upsert`` option is set to ``true``, the driver inserts a |
| 161 | +new document that has the fields and values specified in the update |
| 162 | +document if the query filter doesn't match any existing documents. |
| 163 | + |
| 164 | +.. literalinclude:: /includes/write/update.kt |
| 165 | + :start-after: start-update-options |
| 166 | + :end-before: end-update-options |
| 167 | + :language: kotlin |
| 168 | + :copyable: |
| 169 | + :dedent: |
| 170 | + |
| 171 | +Return Value |
| 172 | +~~~~~~~~~~~~ |
| 173 | + |
| 174 | +The ``updateOne()`` and ``updateMany()`` methods each return an ``UpdateResult`` |
| 175 | +object. You can use the following methods to access information from |
| 176 | +an ``UpdateResult`` instance: |
| 177 | + |
| 178 | +.. list-table:: |
| 179 | + :widths: 30 70 |
| 180 | + :header-rows: 1 |
| 181 | + |
| 182 | + * - Property |
| 183 | + - Description |
| 184 | + |
| 185 | + * - ``getMatchedCount()`` |
| 186 | + - | Indicates the number of documents that matched the query filter, regardless of |
| 187 | + how many updates were performed. |
| 188 | + |
| 189 | + * - ``getModifiedCount()`` |
| 190 | + - | Indicates number of documents modified by the update operation. If an updated |
| 191 | + document is identical to the original, it is not included in this |
| 192 | + count. |
| 193 | + |
| 194 | + * - ``wasAcknowledged()`` |
| 195 | + - | Returns ``true`` if the server acknowledged the result. |
| 196 | + |
| 197 | + * - ``getUpsertedId()`` |
| 198 | + - | Specifies the ``_id`` value of the document that was upserted |
| 199 | + in the database, if the driver performed an upsert. |
| 200 | + |
| 201 | +.. note:: |
| 202 | + |
| 203 | + If the ``wasAcknowledged()`` method returns ``false``, trying to |
| 204 | + access other information from the ``UpdateResult`` instance results in an |
| 205 | + ``InvalidOperation`` exception. The driver cannot |
| 206 | + determine these values if the server does not acknowledge the write |
| 207 | + operation. |
| 208 | + |
| 209 | +Additional Information |
| 210 | +---------------------- |
| 211 | + |
| 212 | +To view runnable code examples that demonstrate how to updates documents by |
| 213 | +using the {+driver-short+}, see :ref:`kotlin-sync-write`. |
| 214 | + |
| 215 | +API Documentation |
| 216 | +~~~~~~~~~~~~~~~~~ |
| 217 | + |
| 218 | +To learn more about any of the methods or types discussed in this |
| 219 | +guide, see the following API documentation: |
| 220 | + |
| 221 | +- `updateOne() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/update-one.html>`__ |
| 222 | +- `updateMany() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/update-many.html>`__ |
| 223 | +- `UpdateOptions <{+core-api+}/com/mongodb/client/model/UpdateOptions.html>`__ |
| 224 | +- `UpdateResult <{+core-api+}/com/mongodb/client/result/UpdateResult.html>`__ |
0 commit comments