Skip to content

Commit f6affeb

Browse files
author
Chris Cho
authored
DOCSP-10317: address feedback on Change a Document (#98)
* DOCSP-10317: address feedback on Change a Document
1 parent 0606a21 commit f6affeb

File tree

1 file changed

+60
-66
lines changed

1 file changed

+60
-66
lines changed

source/fundamentals/crud/write-operations/change-a-document.txt

Lines changed: 60 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@ Change a Document
77
Overview
88
--------
99

10-
You can alter documents in a MongoDB collection with two distinct
11-
operation types: update and replace. Update operations mutate specified
12-
fields in altered documents, but leave other fields and values
13-
unchanged. Replace operations remove all existing fields in altered
14-
documents and substitute the specified fields and values instead.
10+
You can change documents in a MongoDB collection using two distinct
11+
operation types: **update** and **replace**. Update operations mutate
12+
specified fields in one or more documents and leave other fields and values
13+
unchanged. Replace operations remove all existing fields in one or more
14+
documents and substitute them with specified fields and values.
1515

1616
.. _updateDocuments:
1717

1818
Update
1919
------
2020

21-
Create an **update document** using :manual:`update operators
22-
</reference/operator/update/>` to
23-
describe the changes you would like to make via an **update** operation.
24-
Update documents use the following form:
21+
To perform an update to one or more documents, create an **update
22+
document** that specifies the **update operator** (the type of update to
23+
perform) and the fields and values that describe the change. Update
24+
documents use the following format:
2525

2626
.. code-block:: javascript
2727

@@ -38,28 +38,26 @@ Update documents use the following form:
3838
}
3939
}
4040

41-
The top level of an update document contains exclusively update
42-
operators. Update operators allow you to
43-
:manual:`set the value of a field to a literal value
44-
</reference/operator/update/set/#up._S_set>`,
45-
:manual:`increment or decrement field values
46-
</reference/operator/update/inc/#up._S_inc>`,
47-
:manual:`rename fields
48-
</reference/operator/update/rename/#up._S_rename>`,
49-
:manual:`remove fields
50-
</reference/operator/update/unset/#up._S_unset>`,
51-
and :manual:`multiply a field value
52-
</reference/operator/update/mul/#up._S_mul>`,
53-
:manual:`among others </reference/operator/update/#id1>`.
54-
55-
Update operators alter only the fields specified in your update
56-
document.
41+
The top level of an update document contains one or more of the following
42+
update operators:
43+
44+
- ``$set`` - replaces the value of a field with a specified one
45+
- ``$inc`` - increments or decrements field values
46+
- ``$rename`` - renames fields
47+
- ``$unset`` - removes fields
48+
- ``$mul`` - multiplies a field value by a specified number
49+
50+
See the MongoDB server manual for a :manual:`complete list of update operators
51+
and their usage </reference/operator/update-field/>`.
52+
53+
The update operators apply only to the fields associated with them in your
54+
update document.
5755

5856
Example
5957
~~~~~~~
6058

61-
Consider a document containing fields containing the english
62-
alphabet of lowercase letters ``a`` through ``z``:
59+
Consider a document of fields containing the English alphabet of lowercase
60+
letters ``a`` through ``z`` and their ordinal values:
6361

6462
.. code-block:: javascript
6563

@@ -73,13 +71,12 @@ alphabet of lowercase letters ``a`` through ``z``:
7371
z: 26,
7472
}
7573

76-
Updating this document with the following code snippet only alters
77-
the ``z`` field, leaving all other fields in place:
74+
If you apply the ``$set`` update operator with a specified value for ``z``,
75+
your update operation might resemble the following:
7876

7977
.. code-block:: javascript
8078

81-
// match only documents containing a field named 'a' with value 1
82-
const filter = { 'a': 1 };
79+
const filter = { _id: 465 };
8380
// update the value of the 'z' field to 42
8481
const updateDocument = {
8582
$set: {
@@ -88,38 +85,37 @@ the ``z`` field, leaving all other fields in place:
8885
};
8986
const result = await collection.updateOne(filter, updateDocument);
9087

91-
This operation produces the following altered document, with an updated
92-
``z`` field value and all other fields left unchanged:
88+
The updated document resembles the following, with an an updated value in
89+
the ``z`` field and all other values unchanged:
9390

9491
.. code-block:: javascript
9592

9693
{
9794
_id: 465,
9895
a: 1,
99-
b: 2,
100-
c: 3,
10196
...
10297
y: 25,
10398
z: 42,
10499
}
105100

106101
If an update operation fails to match any documents in a collection, it
107-
will do nothing by default. However, update operations can be configured
108-
to :doc:`upsert </fundamentals/crud/write-operations/upsert>`, or
109-
"update or insert", which changes this behavior to instead insert a new
110-
document if no matching document is found. Update operations cannot
111-
change the ``_id`` field of a document. Update operations fail if the
112-
updated document violates a :manual:`unique index
113-
</core/index-unique/>`.
102+
does not make any changes. Update operations can be configured to perform
103+
an :doc:`upsert </fundamentals/crud/write-operations/upsert>` which
104+
attempts to perform an update, but if no documents are matched, inserts
105+
a new document.
106+
107+
You cannot modify the ``_id`` field of a document nor change a field to
108+
a value that violates a unique index constraint. See the MongoDB server manual
109+
for more information on :manual:`unique indexes </core/index-unique/>`.
114110

115111
.. _replacementDocument:
116112

117113
Replace
118114
-------
119115

120-
Create a **replacement document** using literal values to describe the
121-
new document you would like to insert via a **replace** operation.
122-
Replacement documents use the following form:
116+
To perform a replacement operation, create a **replacement document** that
117+
consists of the fields and values that you would like to insert in your
118+
**replace** operation. Replacement documents use the following format:
123119

124120
.. code-block:: javascript
125121

@@ -132,15 +128,14 @@ Replacement documents use the following form:
132128
}
133129
}
134130

135-
Replacement documents contain field names and the values you would like
136-
to assign to those fields. The altered document contains only the
137-
fields listed in the replacement document after a replacement operation.
131+
Replacement documents are the documents that you want to take the place of
132+
existing documents that match the query filters.
138133

139134
Example
140135
~~~~~~~
141136

142-
Consider a document containing fields containing the english
143-
alphabet of lowercase letters ``a`` through ``z``:
137+
Consider a document of fields containing the English alphabet of lowercase
138+
letters ``a`` through ``z`` and their ordinal values:
144139

145140
.. code-block:: javascript
146141

@@ -154,24 +149,21 @@ alphabet of lowercase letters ``a`` through ``z``:
154149
z: 26,
155150
}
156151

157-
Replacing this document with the following code snippet removes all
158-
fields besides the ``z`` field, including the ``a`` field matched by
159-
the filter:
152+
Suppose you wanted to replace this document with one that contains only
153+
the field ``z`` with a value of ``42``. Your replacement operation might
154+
resemble the following:
160155

161156
.. code-block:: javascript
162157

163-
// match only documents containing a field named 'a' with value 1
164-
const filter = { 'a': 1 };
165-
// replace the matched document with a document containing only
166-
// the field 'z' with value 42
158+
const filter = { _id: 465 };
159+
// replace the matched document with the replacement document
167160
const replacementDocument = {
168161
z: 42,
169162
};
170163
const result = await collection.replaceOne(filter, replacementDocument);
171164

172-
This produces the following document. Note that only the ``z`` field,
173-
which was present in the replacement document, and the immutable ``_id``
174-
field, which cannot be changed by any operation, remain:
165+
The replaced document contains the contents of the replacement document
166+
and the immutable ``_id`` field as follows:
175167

176168
.. code-block:: javascript
177169

@@ -181,9 +173,11 @@ field, which cannot be changed by any operation, remain:
181173
}
182174

183175
If a replace operation fails to match any documents in a collection, it
184-
will do nothing by default. However, replace operations can be configured
185-
to :doc:`upsert </fundamentals/crud/write-operations/upsert>`, or
186-
"update or insert", which changes this behavior to instead insert a new
187-
document if no matching document is found. Replace operations fail if
188-
the updated document violates a :manual:`unique index
189-
</core/index-unique/>`.
176+
does not make any changes. Replace operations can be configured to perform
177+
an :doc:`upsert </fundamentals/crud/write-operations/upsert>` which
178+
attempts to perform the replacement, but if no documents are matched, it
179+
inserts a new document.
180+
181+
You cannot modify the ``_id`` field of a document nor change a field to
182+
a value that violates a unique index constraint. See the MongoDB server manual
183+
for more information on :manual:`unique indexes </core/index-unique/>`.

0 commit comments

Comments
 (0)