|
| 1 | +ref: _upsert-unique-index-base |
| 2 | +content: | |
| 3 | +
|
| 4 | + When using the {{upsert}} option with the {{command}} |
| 5 | + {{commandOrMethod}}, **and not** using a :ref:`unique index |
| 6 | + <index-type-unique>` on the query field(s), multiple |
| 7 | + instances of {{aOrAn}} {{command}} operation with similar query |
| 8 | + field(s) could result in duplicate documents being inserted in |
| 9 | + certain circumstances. |
| 10 | +
|
| 11 | + Consider an example where no document with the name ``Andy`` exists |
| 12 | + and multiple clients issue the following command at roughly the same |
| 13 | + time: |
| 14 | +
|
| 15 | + {{codeExample}} |
| 16 | +
|
| 17 | + If all {{command}} operations finish the query phase |
| 18 | + before any client successfully inserts data, **and** there is no |
| 19 | + :ref:`unique index <index-type-unique>` on the ``name`` field, each |
| 20 | + {{command}} operation may result in an insert, creating multiple |
| 21 | + documents with ``name: Andy``. |
| 22 | +
|
| 23 | + To ensure that only one such document is created, and the other |
| 24 | + {{command}} operations update this new document instead, create a |
| 25 | + :ref:`unique index <index-type-unique>` on the ``name`` field. This |
| 26 | + guarantees that only one document with ``name: Andy`` is permitted |
| 27 | + in the collection. |
| 28 | + |
| 29 | + With this unique index in place, the multiple {{command}} operations |
| 30 | + now exhibit the following behavior: |
| 31 | +
|
| 32 | + - Exactly one {{command}} operation will successfully insert a new |
| 33 | + document. |
| 34 | +
|
| 35 | + - All other {{command}} operations will update the newly-inserted |
| 36 | + document, incrementing the ``score`` value. |
| 37 | +
|
| 38 | +--- |
| 39 | +ref: upsert-unique-index-findAndModify-command |
| 40 | +source: |
| 41 | + file: extracts-upsert-unique-index.yaml |
| 42 | + ref: _upsert-unique-index-base |
| 43 | +replacement: |
| 44 | + command: ":dbcommand:`findAndModify`" |
| 45 | + commandOrMethod: "command" |
| 46 | + aOrAn: "a" |
| 47 | + upsert: "``upsert: true``" |
| 48 | + codeExample: | |
| 49 | +
|
| 50 | + .. code-block:: javascript |
| 51 | +
|
| 52 | + db.runCommand( |
| 53 | + { |
| 54 | + findAndModify: "people", |
| 55 | + query: { name: "Andy" }, |
| 56 | + update: { $inc: { score: 1 } }, |
| 57 | + upsert: true |
| 58 | + } |
| 59 | + ) |
| 60 | +
|
| 61 | +--- |
| 62 | +ref: upsert-unique-index-findAndModify-method |
| 63 | +source: |
| 64 | + file: extracts-upsert-unique-index.yaml |
| 65 | + ref: _upsert-unique-index-base |
| 66 | +replacement: |
| 67 | + command: ":method:`~db.collection.findOneAndUpdate()`" |
| 68 | + commandOrMethod: "method" |
| 69 | + aOrAn: "a" |
| 70 | + upsert: "``upsert: true``" |
| 71 | + codeExample: | |
| 72 | +
|
| 73 | + .. code-block:: javascript |
| 74 | +
|
| 75 | + db.people.findAndModify( |
| 76 | + { |
| 77 | + query: { name: "Andy" }, |
| 78 | + update: { $inc: { score: 1 } }, |
| 79 | + upsert: true |
| 80 | + } |
| 81 | + ) |
| 82 | +
|
| 83 | +--- |
| 84 | +ref: upsert-unique-index-update-command |
| 85 | +source: |
| 86 | + file: extracts-upsert-unique-index.yaml |
| 87 | + ref: _upsert-unique-index-base |
| 88 | +replacement: |
| 89 | + command: ":dbcommand:`update`" |
| 90 | + commandOrMethod: "command" |
| 91 | + aOrAn: "an" |
| 92 | + upsert: ":ref:`upsert: true <update-command-upsert>`" |
| 93 | + codeExample: | |
| 94 | +
|
| 95 | + .. code-block:: javascript |
| 96 | +
|
| 97 | + db.runCommand( |
| 98 | + { |
| 99 | + update: "people", |
| 100 | + updates: [ |
| 101 | + { q: { name: "Andy" }, u: { $inc: { score: 1 } }, multi: true, upsert: true } |
| 102 | + ] |
| 103 | + } |
| 104 | + ) |
| 105 | +
|
| 106 | +--- |
| 107 | +ref: upsert-unique-index-update-method |
| 108 | +source: |
| 109 | + file: extracts-upsert-unique-index.yaml |
| 110 | + ref: _upsert-unique-index-base |
| 111 | +replacement: |
| 112 | + command: ":method:`~db.collection.update()`" |
| 113 | + commandOrMethod: "method" |
| 114 | + aOrAn: "a" |
| 115 | + upsert: ":ref:`upsert: true <update-upsert>`" |
| 116 | + codeExample: | |
| 117 | +
|
| 118 | + .. code-block:: javascript |
| 119 | +
|
| 120 | + db.people.update( |
| 121 | + { name: "Andy" }, |
| 122 | + { $inc: { score: 1 } }, |
| 123 | + { |
| 124 | + upsert: true, |
| 125 | + multi: true |
| 126 | + } |
| 127 | + ) |
| 128 | +
|
| 129 | +... |
0 commit comments