Skip to content

Commit 0606a21

Browse files
author
Chris Cho
authored
DOCSP-10319: address feedback on insert or update (#99)
1 parent 10fa2c3 commit 0606a21

File tree

1 file changed

+64
-37
lines changed
  • source/fundamentals/crud/write-operations

1 file changed

+64
-37
lines changed

source/fundamentals/crud/write-operations/upsert.txt

Lines changed: 64 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,70 @@ Insert or Update in a Single Operation
44

55
.. default-domain:: mongodb
66

7-
Many applications require the ability to insert new documents into a
8-
collection as well as the ability to update a document already within
9-
that collection. However, certain workflows benefit from the ability to
10-
complete *either* of these operations depending on whether or not a
11-
matching document already exists in the collection. This can simplify
12-
application logic and remove the need for initialization logic for
13-
certain kinds of document.
14-
15-
Consider a messaging application that keeps track of users using phone
16-
numbers as a unique identifier. When a new user signs up for an account,
17-
it is possible that an older account was already created by a user who
18-
had that phone number in the past, since phone numbers are often reused.
19-
If the application relied solely upon an insert for creating a new
20-
account, the insert would fail, since that phone number was already in
21-
the collection. But if that same application used an upsert, the insert
22-
could succeed.
23-
24-
While some workflows only require the ability to insert or update
25-
documents as discrete operations, many applications benefit from the
26-
ability to either update a document if a matching document is found or
27-
insert a new document if no matching document is found. For this reason,
28-
the ``upsert`` option exists for the :doc:`updateOne()
29-
</usage-examples/updateOne>`, :doc:`replaceOne()
30-
</usage-examples/replaceOne>`, and :doc:`updateMany()
31-
</usage-examples/updateMany>` methods. Set this option to ``true`` in
32-
the ``options`` object passed to either method to insert a document if
33-
no matching document is found. If a matching document is not found for
34-
the ``updateOne()`` or ``updateMany()`` methods, literal values in the
35-
query document are used to instantiate a new document and the update
36-
document is subsequently used to alter that document.
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:
12+
13+
- :doc:`updateOne() </usage-examples/updateOne>`
14+
- :doc:`replaceOne() </usage-examples/replaceOne>`
15+
- :doc:`updateMany() </usage-examples/updateMany>`
16+
17+
If the query filter passed to these methods does not find any matches and
18+
you set the ``upsert`` option to ``true``, MongoDB inserts the update
19+
document. Let's go through an example.
20+
21+
Suppose your application tracks the current location of food trucks,
22+
storing the nearest address data in a MongoDB collection that resembles the
23+
following:
24+
25+
.. code-block:: javascript
26+
27+
[
28+
{ name: "Haute Skillet", address: "42 Avenue B" },
29+
{ name: "Lady of the Latke", address: "35 Fulton Rd" },
30+
...
31+
]
32+
33+
34+
As an application user, you read about a food truck changing its regular
35+
location and want to apply the update. This update might resemble the
36+
following:
37+
38+
.. code-block:: javascript
39+
40+
const query = { name: "Deli Llama" };
41+
const update = { name: "Deli Llama", address: "3 Nassau St" };
42+
const options = {};
43+
collection.updateOne(query, update, options);
44+
45+
If a food truck named "Deli Llama" exists, the method call above updates
46+
the document in the collection. However, if there are no food trucks named
47+
"Deli Llama" in your collection, no changes are made.
48+
49+
Consider the case in which you want to add information about the food
50+
truck even if it does not currently exist in your collection. Rather than
51+
first querying whether it exists to determine whether we need to insert or
52+
update the document, we can set ``upsert`` to ``true`` in our call to
53+
``updateOne()`` as follows:
54+
55+
.. code-block:: javascript
56+
57+
const query = { name: "Deli Llama" };
58+
const update = { name: "Deli Llama", address: "3 Nassau St" };
59+
const options = { upsert: true };
60+
collection.updateOne(query, update, options);
61+
62+
After you run the operation above, your collection should resemble the
63+
following, whether the "Deli Llama" document existed in your collection
64+
beforehand:
3765

3866
.. code-block:: javascript
3967

40-
// define an empty query document
41-
const query = {"address":"1780 State Route 860"};
42-
const update = {"address":"42 Avenue B"};
43-
const options = {
44-
upsert: true
45-
}
46-
collection.updateOne(query, update, options)
68+
[
69+
{ name: "Haute Skillet", address: "42 Avenue B" },
70+
{ name: "Lady of the Latke", address: "35 Fulton Rd" },
71+
{ name: "Deli Llama", address: "3 Nassau St" },
72+
...
73+
]

0 commit comments

Comments
 (0)