|
| 1 | +.. _ruby-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 ``update_one`` and |
| 25 | +``update_many`` methods. |
| 26 | + |
| 27 | +Sample Data |
| 28 | +~~~~~~~~~~~ |
| 29 | + |
| 30 | +The examples in this guide use the ``restaurants`` collection in the ``sample_restaurants`` |
| 31 | +database from the :atlas:`Atlas sample datasets </sample-data>`. To access this collection |
| 32 | +from your {+language+} application, create a ``Mongo::Client`` object that connects to an Atlas cluster |
| 33 | +and assign the following values to your ``database`` and ``collection`` variables: |
| 34 | + |
| 35 | +.. literalinclude:: /includes/write/update.rb |
| 36 | + :language: ruby |
| 37 | + :dedent: |
| 38 | + :start-after: start-db-coll |
| 39 | + :end-before: end-db-coll |
| 40 | + |
| 41 | +To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the |
| 42 | +:atlas:`Get Started with Atlas </getting-started>` guide. |
| 43 | + |
| 44 | +Update Operations |
| 45 | +----------------- |
| 46 | + |
| 47 | +You can update documents in MongoDB by using the following methods: |
| 48 | + |
| 49 | +- ``update_one``: Updates *the first document* that matches the search criteria |
| 50 | +- ``update_many``: Updates *all documents* that match the search criteria |
| 51 | + |
| 52 | +Each update method requires the following parameters: |
| 53 | + |
| 54 | +- **Query filter**, which matches the documents you want to update. To learn |
| 55 | + more about query filters, see the :ref:`ruby-specify-query` |
| 56 | + guide. |
| 57 | + |
| 58 | +- **Update document**, which specifies the update operator and the fields and values to be |
| 59 | + updated. The update operator specifies the type of update to perform. To view a list of |
| 60 | + update operators and learn about their usages, see the |
| 61 | + :manual:`Field Update Operators guide page</reference/operator/update-field/>` in the |
| 62 | + {+mdb-server+} manual. |
| 63 | + |
| 64 | +Update One Document Example |
| 65 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 66 | + |
| 67 | +The following example uses the ``update_one`` method to find the first document |
| 68 | +where the value of the ``name`` field is ``"Happy Garden"``. It then uses the ``$set`` |
| 69 | +operator to update the ``name`` field value to ``"Mountain House"``. |
| 70 | + |
| 71 | +.. io-code-block:: |
| 72 | + :copyable: true |
| 73 | + |
| 74 | + .. input:: /includes/write/update.rb |
| 75 | + :start-after: start-update-one |
| 76 | + :end-before: end-update-one |
| 77 | + :language: ruby |
| 78 | + :dedent: |
| 79 | + |
| 80 | + .. output:: |
| 81 | + :language: console |
| 82 | + :visible: false |
| 83 | + |
| 84 | + 1 document(s) updated |
| 85 | + |
| 86 | +Update Many Documents Example |
| 87 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 88 | + |
| 89 | +The following example uses the ``update_many`` method to update all documents |
| 90 | +where the value of the ``name`` field is ``"Starbucks"``. The update document uses the |
| 91 | +``$rename`` operator to change the name of the ``address`` field to ``location``. |
| 92 | + |
| 93 | +.. io-code-block:: |
| 94 | + :copyable: true |
| 95 | + |
| 96 | + .. input:: /includes/write/update.rb |
| 97 | + :start-after: start-update-many |
| 98 | + :end-before: end-update-many |
| 99 | + :language: ruby |
| 100 | + :dedent: |
| 101 | + |
| 102 | + .. output:: |
| 103 | + :language: console |
| 104 | + :visible: false |
| 105 | + |
| 106 | + 11 document(s) updated |
| 107 | + |
| 108 | +Customize the Update Operation |
| 109 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 110 | + |
| 111 | +The ``update_one`` and ``update_many`` methods accept options to configure the update |
| 112 | +operation. You can pass these options individually as parameters, or you can create a |
| 113 | +``Hash`` object that contains the options and pass the object as a parameter. |
| 114 | +If you don't specify any options, the driver performs the update |
| 115 | +operation with default settings. |
| 116 | + |
| 117 | +The following table describes the options that you can use to |
| 118 | +configure the update operation: |
| 119 | + |
| 120 | +.. list-table:: |
| 121 | + :widths: 30 70 |
| 122 | + :header-rows: 1 |
| 123 | + |
| 124 | + * - Option |
| 125 | + - Description |
| 126 | + |
| 127 | + * - ``upsert`` |
| 128 | + - | Whether the update operation performs an upsert operation if no |
| 129 | + documents match the query filter. For more information, see the :manual:`upsert |
| 130 | + statement </reference/command/update/#std-label-update-command-upsert>` |
| 131 | + in the {+mdb-server+} manual. |
| 132 | + | Default: ``false`` |
| 133 | + |
| 134 | + * - ``bypass_document_validation`` |
| 135 | + - | Whether the update operation bypasses document validation. This lets you |
| 136 | + update documents that don't meet the schema validation requirements, if any |
| 137 | + exist. For more information about schema validation, see :manual:`Schema |
| 138 | + Validation </core/schema-validation/#schema-validation>` in the MongoDB |
| 139 | + Server manual. |
| 140 | + | Default: ``false`` |
| 141 | + |
| 142 | + * - ``collation`` |
| 143 | + - | Language collation to use when sorting |
| 144 | + results. For more information, see :manual:`Collation </reference/collation/#std-label-collation>` |
| 145 | + in the {+mdb-server+} manual. |
| 146 | + |
| 147 | + * - ``array_filters`` |
| 148 | + - | List of filters that you specify to select which |
| 149 | + array elements the update applies to. |
| 150 | + |
| 151 | + * - ``hint`` |
| 152 | + - | Index to use when matching documents. |
| 153 | + For more information, see the :manual:`hint statement </reference/command/update/#std-label-update-command-hint>` |
| 154 | + in the {+mdb-server+} manual. |
| 155 | + |
| 156 | + * - ``let`` |
| 157 | + - | Map of parameter names and values to set top-level |
| 158 | + variables for the operation. Values must be constant or closed |
| 159 | + expressions that don't reference document fields. For more information, |
| 160 | + see the :manual:`let statement |
| 161 | + </reference/command/update/#std-label-update-let-syntax>` in the |
| 162 | + {+mdb-server+} manual. |
| 163 | + |
| 164 | +Modify Update Example |
| 165 | +````````````````````` |
| 166 | + |
| 167 | +The example uses the ``$equal`` operator to match documents |
| 168 | +where the value of the ``name`` field is ``"Sunrise Pizzeria"``. It then uses the ``$set`` |
| 169 | +operator to set the ``borough`` field value in the first matching document to |
| 170 | +``"Queens"`` and the ``cuisine`` field value to ``"Italian"``. |
| 171 | + |
| 172 | +Because the ``upsert`` option is set to ``true``, if the query filter |
| 173 | +doesn't match any existing documents, the driver inserts a new document that |
| 174 | +contains the fields and values in the filter and update documents. |
| 175 | + |
| 176 | +.. io-code-block:: |
| 177 | + :copyable: true |
| 178 | + |
| 179 | + .. input:: /includes/write/update.rb |
| 180 | + :start-after: start-update-options |
| 181 | + :end-before: end-update-options |
| 182 | + :language: ruby |
| 183 | + :dedent: |
| 184 | + |
| 185 | + .. output:: |
| 186 | + :language: console |
| 187 | + :visible: false |
| 188 | + |
| 189 | + 1 document(s) updated |
| 190 | + |
| 191 | +Return Value |
| 192 | +~~~~~~~~~~~~ |
| 193 | + |
| 194 | +The ``update_one`` and ``update_many`` methods each return a ``Result`` |
| 195 | +object. You can access the following methods from a ``Result`` instance: |
| 196 | + |
| 197 | +.. list-table:: |
| 198 | + :widths: 30 70 |
| 199 | + :header-rows: 1 |
| 200 | + |
| 201 | + * - Method |
| 202 | + - Description |
| 203 | + |
| 204 | + * - ``matched_count`` |
| 205 | + - | Number of documents that matched the query filter, regardless of |
| 206 | + how many updates were performed. |
| 207 | + |
| 208 | + * - ``modified_count`` |
| 209 | + - | Number of documents modified by the update operation. If an updated |
| 210 | + document is identical to the original, it is not included in this |
| 211 | + count. |
| 212 | + |
| 213 | + * - ``acknowledged?`` |
| 214 | + - | Returns ``true`` if the server acknowledged the result. |
| 215 | + |
| 216 | + * - ``upserted_count`` |
| 217 | + - | Returns the number of documents that were upserted in the database, if the driver |
| 218 | + performed an upsert. |
| 219 | + |
| 220 | + * - ``upserted_ids`` |
| 221 | + - | Returns the ``_id`` value of the document that was upserted |
| 222 | + in the database, if the driver performed an upsert. |
| 223 | + |
| 224 | +.. tip:: |
| 225 | + |
| 226 | + Check the value of the ``acknowledged?`` method before you try |
| 227 | + to call any other ``Result`` methods. If the ``acknowledged?`` |
| 228 | + method returns ``false``, the driver throws an ``InvalidOperation`` |
| 229 | + exception if you try to call any other method on the ``Result`` object. |
| 230 | + The driver cannot determine these values if the server does not acknowledge the write |
| 231 | + operation. |
| 232 | + |
| 233 | +Additional Information |
| 234 | +---------------------- |
| 235 | + |
| 236 | +To view runnable code examples that demonstrate how to update documents by |
| 237 | +using the {+driver-short+}, see :ref:`ruby-write`. |
| 238 | + |
| 239 | +API Documentation |
| 240 | +~~~~~~~~~~~~~~~~~ |
| 241 | + |
| 242 | +To learn more about any of the methods or types discussed in this |
| 243 | +guide, see the following API documentation: |
| 244 | + |
| 245 | +- `update_one <{+api-root+}/Mongo/Collection.html#update_one-instance_method>`_ |
| 246 | +- `update_many <{+api-root+}/Mongo/Collection.html#update_many-instance_method>`_ |
| 247 | +- `Result <{+api-root+}/Mongo/Operation/Update/Result.html>`_ |
0 commit comments