|
2 | 2 | Insert or Update in a Single Operation
|
3 | 3 | ======================================
|
4 | 4 |
|
5 |
| -.. default-domain:: mongodb |
| 5 | +Applications use insert and update operations to store and modify data. |
| 6 | +Sometimes, you need to choose between an insert and update depending on |
| 7 | +whether the document exists. MongoDB simplifies this decision for us |
| 8 | +with an ``upsert`` option. |
6 | 9 |
|
7 |
| -If your application stores and modifies data in MongoDB, you probably use |
8 |
| -insert and update operations. In certain workflows, you may need to choose |
9 |
| -between an insert and update depending on whether the document exists. |
10 |
| -In these cases, you can streamline your application logic by using the |
11 |
| -``upsert`` option available in the following methods: |
| 10 | +An ``upsert``: |
| 11 | + |
| 12 | +- Updates documents that match your query filter |
| 13 | +- Inserts a document if there are no matches to your query filter |
| 14 | + |
| 15 | +Specify an Upsert: |
| 16 | +------------------ |
| 17 | + |
| 18 | +To specify an upsert with the ``updateOne()`` or ``updateMany()`` |
| 19 | +methods, pass ``true`` to :java-docs:`UpdateOptions.upsert() |
| 20 | +</apidocs/mongodb-driver-core/com/mongodb/client/model/UpdateOptions.html#upsert(boolean)>`. |
| 21 | + |
| 22 | +To specify an upsert with the ``replaceOne()`` method, pass ``true`` to |
| 23 | +:java-docs:`ReplaceOptions.upsert() |
| 24 | +</apidocs/mongodb-driver-core/com/mongodb/client/model/ReplaceOptions.html#upsert(boolean)>`. |
| 25 | + |
| 26 | +In the following example, a paint store sells eight different |
| 27 | +colors of paint. The store had their annual online sale. Their |
| 28 | +``paint_inventory`` collection now shows the following documents: |
| 29 | + |
| 30 | +.. code-block:: json |
| 31 | + |
| 32 | + { "_id": { "$oid": "606b4cfbcd83be7518b958da" }, "color": "red", "qty": 5 } |
| 33 | + { "_id": { "$oid": "606b4cfbcd83be7518b958db" }, "color": "purple", "qty": 8 } |
| 34 | + { "_id": { "$oid": "606b4cfbcd83be7518b958dc" }, "color": "blue", "qty": 0 } |
| 35 | + { "_id": { "$oid": "606b4cfbcd83be7518b958dd" }, "color": "white", "qty": 0 } |
| 36 | + { "_id": { "$oid": "606b4cfbcd83be7518b958de" }, "color": "yellow", "qty": 6 } |
| 37 | + { "_id": { "$oid": "606b4cfbcd83be7518b958df" }, "color": "pink", "qty": 0 } |
| 38 | + { "_id": { "$oid": "606b4cfbcd83be7518b958e0" }, "color": "green", "qty": 0 } |
| 39 | + { "_id": { "$oid": "606b4cfbcd83be7518b958e1" }, "color": "black", "qty": 8 } |
| 40 | + |
| 41 | +The store received a fresh shipment and needs to update their inventory. |
| 42 | +The first item in the shipment is ten cans of orange paint. |
| 43 | + |
| 44 | +To update the inventory, query the ``paint_inventory`` collection |
| 45 | +where the ``color`` is ``"orange"``, specify an update to ``increment`` the |
| 46 | +``qty`` field by ``10``, and specify ``true`` to |
| 47 | +``UpdateOptions.upsert()``: |
| 48 | + |
| 49 | +.. literalinclude:: /includes/fundamentals/code-snippets/InsertUpdate.java |
| 50 | + :language: java |
| 51 | + :dedent: |
| 52 | + :start-after: begin updateOneExample |
| 53 | + :end-before: end updateOneExample |
| 54 | + |
| 55 | +The method returns: |
| 56 | + |
| 57 | +.. code-block:: json |
| 58 | + |
| 59 | + AcknowledgedUpdateResult{ matchedCount=0, modifiedCount=0, upsertedId=BsonObjectId{ value=606b4cfc1601f9443b5d6978 }} |
| 60 | + |
| 61 | +This ``AcknowledgedUpdateResult`` tells us: |
| 62 | + |
| 63 | +- Zero documents matched our query filter |
| 64 | +- Zero documents in our collection got modified |
| 65 | +- A document with an ``_id`` of ``606b4cfc1601f9443b5d6978`` got upserted |
| 66 | + |
| 67 | +The following shows the documents in the ``paint_inventory`` collection: |
| 68 | + |
| 69 | +.. code-block:: json |
| 70 | + |
| 71 | + { "_id": { "$oid": "606b4cfbcd83be7518b958da" }, "color": "red", "qty": 5 } |
| 72 | + { "_id": { "$oid": "606b4cfbcd83be7518b958db" }, "color": "purple", "qty": 8 } |
| 73 | + { "_id": { "$oid": "606b4cfbcd83be7518b958dc" }, "color": "blue", "qty": 0 } |
| 74 | + { "_id": { "$oid": "606b4cfbcd83be7518b958dd" }, "color": "white", "qty": 0 } |
| 75 | + { "_id": { "$oid": "606b4cfbcd83be7518b958de" }, "color": "yellow", "qty": 6 } |
| 76 | + { "_id": { "$oid": "606b4cfbcd83be7518b958df" }, "color": "pink", "qty": 0 } |
| 77 | + { "_id": { "$oid": "606b4cfbcd83be7518b958e0" }, "color": "green", "qty": 0 } |
| 78 | + { "_id": { "$oid": "606b4cfbcd83be7518b958e1" }, "color": "black", "qty": 8 } |
| 79 | + { "_id": { "$oid": "606b4cfc1601f9443b5d6978" }, "color": "orange", "qty": 10 }] |
| 80 | + |
| 81 | +.. note:: |
| 82 | + |
| 83 | + Not including ``UpdateOptions`` results in no change to the collection. |
| 84 | + |
| 85 | + .. literalinclude:: /includes/fundamentals/code-snippets/InsertUpdate.java |
| 86 | + :language: java |
| 87 | + :dedent: |
| 88 | + :start-after: begin updateOneAttemptExample |
| 89 | + :end-before: end updateOneAttemptExample |
| 90 | + |
| 91 | + The method returns: |
| 92 | + |
| 93 | + .. code-block:: json |
| 94 | + |
| 95 | + AcknowledgedUpdateResult{ matchedCount=0, modifiedCount=0, upsertedId=null } |
0 commit comments