Skip to content

Commit 19e9e1d

Browse files
author
Dave
authored
DOCSP-16178 BACKPORT (#871)
1 parent 8adfe74 commit 19e9e1d

File tree

2 files changed

+131
-159
lines changed

2 files changed

+131
-159
lines changed

source/reference/method/db.collection.findOneAndReplace.txt

Lines changed: 64 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -42,145 +42,130 @@ form:
4242
sort: <document>,
4343
maxTimeMS: <number>,
4444
upsert: <boolean>,
45+
returnDocument: <string>,
4546
returnNewDocument: <boolean>,
4647
collation: <document>
4748
}
4849
)
4950

50-
Parameters and Options
51-
~~~~~~~~~~~~~~~~~~~~~~
51+
Fields and Options
52+
~~~~~~~~~~~~~~~~~~
5253

5354
The :method:`~db.collection.findOneAndReplace()` method takes the following
54-
parameters and options:
55+
fields and options:
5556

5657

5758
.. list-table::
5859
:header-rows: 1
5960
:widths: 20 20 80
6061

61-
* - Parameter
62-
62+
* - Field
6363
- Type
64-
6564
- Description
6665

6766
* - :ref:`filter <findOneAndReplace-filter>`
68-
6967
- document
70-
7168
- .. _findOneAndReplace-filter:
72-
69+
7370
The selection criteria for the update. The same :ref:`query
7471
selectors <query-selectors>` as in the :method:`find()
7572
<db.collection.find()>` method are available.
76-
77-
Specify an empty document ``{ }`` to replace the first document returned in
78-
the collection.
79-
73+
74+
To replace the first document returned in the collection,
75+
specify an empty document ``{ }``.
76+
8077
If unspecified, defaults to an empty document.
81-
82-
Starting in MongoDB 4.2 (and 4.0.12+, 3.6.14+, and 3.4.23+), the operation
83-
errors if the query argument is not a document.
84-
85-
8678

87-
* - :ref:`replacement <findOneAndReplace-replacement>`
79+
Starting in MongoDB 4.2, the operation returns an error if the
80+
query argument is not a document.
8881

82+
* - :ref:`replacement <findOneAndReplace-replacement>`
8983
- document
90-
9184
- .. _findOneAndReplace-replacement:
92-
85+
9386
The replacement document.
94-
87+
9588
Cannot contain
96-
:doc:`update operators</reference/operator/update>`.
97-
98-
The ``<replacement>`` document cannot specify an ``_id`` value that differs
99-
from the replaced document.
100-
101-
89+
:ref:`update operators <update-operators-top-level>`.
10290

103-
* - :ref:`projection <findOneAndReplace-projection>`
91+
The ``<replacement>`` document cannot specify an ``_id`` value
92+
that differs from the replaced document.
10493

94+
* - :ref:`projection <findOneAndReplace-projection>`
10595
- document
106-
10796
- .. _findOneAndReplace-projection:
108-
97+
10998
Optional. A subset of fields to return.
110-
111-
To return all fields in the matching document, omit this parameter.
112-
113-
Starting in MongoDB 4.2 (and 4.0.12+, 3.6.14+, and 3.4.23+), the operation
114-
errors if the projection argument is not a document.
115-
116-
11799

118-
* - :ref:`sort <findOneAndReplace-sort>`
100+
To return all fields in the matching document, omit this
101+
field.
119102

120-
- document
103+
Starting in MongoDB 4.2, the operation returns an error if the
104+
projection field is not a document.
121105

106+
* - :ref:`sort <findOneAndReplace-sort>`
107+
- document
122108
- .. _findOneAndReplace-sort:
123-
124-
Optional. Specifies a sorting order for the documents matched by the ``filter``.
109+
110+
Optional. Specifies a sorting order for the documents matched by
111+
the :ref:`filter <findOneAndReplace-filter>`.
125112

126-
Starting in MongoDB 4.2 (and 4.0.12+, 3.6.14+, and 3.4.23+), the operation
127-
errors if the sort argument is not a document.
113+
Starting in MongoDB 4.2, the operation returns an error if the
114+
sort field is not a document.
128115

129116
See :method:`cursor.sort()`.
130-
131-
132117

133118
* - ``maxTimeMS``
134-
135119
- number
136-
137-
- Optional. Specifies a time limit in milliseconds within which the operation must
138-
complete. Throws an error if the limit is exceeded.
139-
140-
120+
- Optional. Specifies a time limit in milliseconds within which
121+
the operation must complete. Returns an error if the limit is
122+
exceeded.
141123

142124
* - :ref:`upsert <findOneAndReplace-upsert>`
143-
144125
- boolean
145-
146126
- .. _findOneAndReplace-upsert:
147127

148128
.. include:: /includes/extracts/findOneAndReplace-behavior-method.rst
149-
150-
151129

152-
* - :ref:`returnNewDocument <findOneAndReplace-returnNewDocument>`
130+
* - :ref:`returnDocument <findOneAndReplace-returnDocument>`
131+
- string
132+
- .. _findOneAndReplace-returnDocument:
153133

154-
- boolean
134+
Optional. Starting in :binary:`mongosh` 0.13.2,
135+
``returnDocument`` is an alternative for
136+
:ref:`returnNewDocument <findOneAndReplace-returnNewDocument>`.
137+
If both options are set, ``returnDocument`` takes precedence.
155138

139+
``returnDocument: "before"`` returns the original document.
140+
``returnDocument: "after"`` returns the updated document.
141+
142+
* - :ref:`returnNewDocument <findOneAndReplace-returnNewDocument>`
143+
- boolean
156144
- .. _findOneAndReplace-returnNewDocument:
157145

158-
Optional. When ``true``, returns the replacement document instead of the original
159-
document.
160-
146+
Optional. When ``true``, returns the replacement document
147+
instead of the original document.
148+
161149
Defaults to ``false``.
162-
163-
164150

165-
* - :ref:`collation <findOneAndReplace-collation>`
166151

152+
* - :ref:`collation <findOneAndReplace-collation>`
167153
- document
168-
169154
- .. _findOneAndReplace-collation:
170155

171156
Optional.
172-
157+
173158
.. include:: /includes/extracts/collation-option.rst
174-
175-
176159

177160

178161
Returns
179162
~~~~~~~
180163

181-
Returns either the original document or, if :ref:`returnNewDocument:
182-
true <findOneAndReplace-returnNewDocument>`, the replacement document.
183-
164+
Returns the original document by default. Returns the updated document
165+
if :ref:`returnDocument <findOneAndReplace-returnDocument>` is set to
166+
``after`` or :ref:`returnNewDocument
167+
<findOneAndReplace-returnNewDocument>` is set to ``true``.
168+
184169
Behavior
185170
--------
186171

@@ -189,14 +174,14 @@ Document Match
189174

190175
:method:`db.collection.findOneAndReplace()` replaces the first matching
191176
document in the collection that matches the ``filter``.
192-
The ``sort`` parameter can be used to influence which document is modified.
177+
The ``sort`` field can be used to influence which document is modified.
193178

194179
Projection
195180
~~~~~~~~~~
196181

197182
.. include:: /includes/extracts/projection-language-consistency-admonition.rst
198183

199-
The ``projection`` parameter takes a document in the following form:
184+
The ``projection`` field takes a document in the following form:
200185

201186
.. code-block:: javascript
202187

@@ -451,17 +436,17 @@ If the operation exceeds the time limit, it returns:
451436
Replace Document with Upsert
452437
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
453438

454-
The following operation uses the :ref:`upsert <findOneAndReplace-upsert>` field to insert the
455-
replacement document if no document matches the :ref:`filter
456-
<findOneAndReplace-filter>`:
439+
The following operation uses the :ref:`upsert
440+
<findOneAndReplace-upsert>` field to insert the replacement document if
441+
no document matches the :ref:`filter <findOneAndReplace-filter>`:
457442

458443
.. code-block:: javascript
459444

460445
try {
461446
db.scores.findOneAndReplace(
462447
{ "team" : "Fortified Lobsters" },
463448
{ "_id" : 6019, "team" : "Fortified Lobsters" , "score" : 32000},
464-
{ upsert : true, returnNewDocument: true }
449+
{ upsert : true, returnDocument: "after" }
465450
);
466451
} catch (e){
467452
print(e);
@@ -477,8 +462,8 @@ The operation returns the following:
477462
"score" : 32000
478463
}
479464

480-
If ``returnNewDocument`` was false, the operation would return ``null`` as
481-
there is no original document to return.
465+
If ``returnDocument: "before"`` was set, the operation would return
466+
``null`` because there is no original document to return.
482467

483468
Specify Collation
484469
~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)