|
| 1 | +.. _ruby-write-replace: |
| 2 | + |
| 3 | +================= |
| 4 | +Replace 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, code example |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to use the {+driver-short+} to perform a **replace |
| 24 | +operation** on a document in a MongoDB collection. A replace operation |
| 25 | +removes all fields and values from a specified document except the |
| 26 | +``_id`` field, and adds new fields and values that you specify. This |
| 27 | +operation differs from an update operation, which changes only |
| 28 | +specified fields in one or more documents. |
| 29 | + |
| 30 | +To learn more about update operations, see the |
| 31 | +:ref:`ruby-write-update` guide. |
| 32 | + |
| 33 | +Sample Data |
| 34 | +~~~~~~~~~~~ |
| 35 | + |
| 36 | +The examples in this guide use the ``restaurants`` collection in the ``sample_restaurants`` |
| 37 | +database from the :atlas:`Atlas sample datasets </sample-data>`. To access this collection |
| 38 | +from your {+language+} application, create a ``MongoClient`` object that connects to an Atlas cluster |
| 39 | +and assign the following values to your ``database`` and ``collection`` variables: |
| 40 | + |
| 41 | +.. literalinclude:: /includes/write/replace.rb |
| 42 | + :language: ruby |
| 43 | + :dedent: |
| 44 | + :start-after: start-db-coll |
| 45 | + :end-before: end-db-coll |
| 46 | + |
| 47 | +To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the |
| 48 | +:atlas:`Get Started with Atlas </getting-started>` guide. |
| 49 | + |
| 50 | +Replace Operation |
| 51 | +----------------- |
| 52 | + |
| 53 | +You can perform a replace operation in MongoDB by using the |
| 54 | +``replace_one`` method. This method removes all fields except the |
| 55 | +``_id`` field from the first document that matches the specified query filter. It |
| 56 | +then adds the fields and values you specify to the empty document. |
| 57 | + |
| 58 | +Required Parameters |
| 59 | +~~~~~~~~~~~~~~~~~~~ |
| 60 | + |
| 61 | +You must pass the following parameters to the ``replace_one`` method: |
| 62 | + |
| 63 | +- **Query filter**: Specifies which documents to update. To learn |
| 64 | + more about query filters, see the :ref:`ruby-specify-query` |
| 65 | + guide. |
| 66 | + |
| 67 | +- **Replacement document**: Specifies the fields and values that |
| 68 | + you want to replace the existing fields and values with. |
| 69 | + |
| 70 | +Replace Example |
| 71 | +~~~~~~~~~~~~~~~ |
| 72 | + |
| 73 | +The following example uses the ``replace_one`` method to replace the |
| 74 | +fields and values of a document in which the value of the ``name`` field |
| 75 | +is ``"Primola Restaurant"``: |
| 76 | + |
| 77 | +.. io-code-block:: |
| 78 | + :copyable: true |
| 79 | + |
| 80 | + .. input:: /includes/write/replace.rb |
| 81 | + :start-after: start-replace-one |
| 82 | + :end-before: end-replace-one |
| 83 | + :language: ruby |
| 84 | + :dedent: |
| 85 | + |
| 86 | + .. output:: |
| 87 | + :language: console |
| 88 | + :visible: false |
| 89 | + |
| 90 | + Replaced 1 document(s) |
| 91 | + |
| 92 | +.. important:: |
| 93 | + |
| 94 | + The value of the ``_id`` field is immutable. If your replacement |
| 95 | + document specifies a value for the ``_id`` field, it must be the same |
| 96 | + as the ``_id`` value of the existing document or the driver raises a |
| 97 | + ``WriteError``. |
| 98 | + |
| 99 | +Customize the Replace Operation |
| 100 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 101 | + |
| 102 | +You can pass a ``Hash`` object as a parameter to the ``replace_one`` method to set options to |
| 103 | +configure the replace operation. If you don't specify any options, the driver performs the replace |
| 104 | +operation with default settings. |
| 105 | + |
| 106 | +The following table describes the options that you can use to |
| 107 | +configure the replace operation: |
| 108 | + |
| 109 | +.. list-table:: |
| 110 | + :widths: 30 70 |
| 111 | + :header-rows: 1 |
| 112 | + |
| 113 | + * - Option |
| 114 | + - Description |
| 115 | + |
| 116 | + * - ``upsert`` |
| 117 | + - | Specifies whether the replace operation performs an upsert operation if no |
| 118 | + documents match the query filter. For more information, see :manual:`upsert |
| 119 | + behavior </reference/method/db.collection.replaceOne/#upsert>` |
| 120 | + in the {+mdb-server+} manual. |
| 121 | + | Defaults to ``false``. |
| 122 | + |
| 123 | + * - ``bypass_document_validation`` |
| 124 | + - | Specifies whether the update operation bypasses document validation. This lets you |
| 125 | + update documents that don't meet the schema validation requirements, if any |
| 126 | + exist. For more information about schema validation, see :manual:`Schema |
| 127 | + Validation </core/schema-validation/#schema-validation>` in the {+mdb-server+} manual. |
| 128 | + | Defaults to ``false``. |
| 129 | + |
| 130 | + * - ``collation`` |
| 131 | + - | Specifies the kind of language collation to use when sorting |
| 132 | + results. For more information, see :manual:`Collation </reference/collation/#std-label-collation>` |
| 133 | + in the {+mdb-server+} manual. |
| 134 | + |
| 135 | + * - ``session`` |
| 136 | + - | Specifies the session to use for the operation. To learn more about sessions, see |
| 137 | + :manual:`Client Sessions and Causal Consistency Guarantees </core/read-isolation-consistency-recency/#std-label-sessions>` |
| 138 | + in the {+mdb-server+} manual. |
| 139 | + |
| 140 | + * - ``hint`` |
| 141 | + - | Sets the index to use when matching documents. |
| 142 | + For more information, see the :manual:`hint statement |
| 143 | + </reference/method/db.collection.replaceOne/#std-label-replace-one-hint>` |
| 144 | + in the {+mdb-server+} manual. |
| 145 | + |
| 146 | + * - ``let`` |
| 147 | + - | Provides a map of parameter names and values to set top-level |
| 148 | + variables for the operation. Values must be constant or closed |
| 149 | + expressions that don't reference document fields. |
| 150 | + |
| 151 | +The following code performs the same replace operation as the preceding example, but sets the ``upsert`` |
| 152 | +option to ``true``. This instructs the driver to insert a new document that has the fields |
| 153 | +and values specified in the replacement document if the query filter doesn't match any |
| 154 | +existing documents: |
| 155 | + |
| 156 | +.. io-code-block:: |
| 157 | + :copyable: true |
| 158 | + |
| 159 | + .. input:: /includes/write/replace.rb |
| 160 | + :start-after: start-replace-options |
| 161 | + :end-before: end-replace-options |
| 162 | + :language: ruby |
| 163 | + :dedent: |
| 164 | + |
| 165 | + .. output:: |
| 166 | + :language: console |
| 167 | + :visible: false |
| 168 | + |
| 169 | + Replaced 1 document(s) |
| 170 | + |
| 171 | +Return Value |
| 172 | +~~~~~~~~~~~~ |
| 173 | + |
| 174 | +The ``replace_one`` method returns a ``Mongo::Operation::Update::Result`` |
| 175 | +object. You can use the following methods to access information from |
| 176 | +a ``Result`` instance: |
| 177 | + |
| 178 | +.. list-table:: |
| 179 | + :widths: 30 70 |
| 180 | + :header-rows: 1 |
| 181 | + |
| 182 | + * - Method |
| 183 | + - Description |
| 184 | + |
| 185 | + * - ``matched_count`` |
| 186 | + - | Returns the number of documents that matched the query filter. |
| 187 | + |
| 188 | + * - ``modified_count`` |
| 189 | + - | Returns the number of documents modified by the update operation. If an updated |
| 190 | + document is identical to the original, it is not included in this |
| 191 | + count. |
| 192 | + |
| 193 | + * - ``upserted_count`` |
| 194 | + - | Returns the number of documents upserted. |
| 195 | + |
| 196 | + * - ``upserted_id`` |
| 197 | + - | Returns the ``_id`` value of the document that the driver upserted |
| 198 | + into the database, if any. |
| 199 | + |
| 200 | +Additional Information |
| 201 | +---------------------- |
| 202 | + |
| 203 | +To view a runnable code example that demonstrates how to replace a |
| 204 | +document, see :ref:`ruby-write`. |
| 205 | + |
| 206 | +API Documentation |
| 207 | +~~~~~~~~~~~~~~~~~ |
| 208 | + |
| 209 | +To learn more about any of the methods or types discussed in this |
| 210 | +guide, see the following API documentation: |
| 211 | + |
| 212 | +- `replace_one <{+api-root+}/Mongo/Collection.html#replace_one-instance_method>`_ |
| 213 | +- `Mongo::Operation::Update::Result <{+api-root+}/Mongo/Operation/Update/Result.html>`_ |
0 commit comments