@@ -42,145 +42,130 @@ form:
42
42
sort: <document>,
43
43
maxTimeMS: <number>,
44
44
upsert: <boolean>,
45
+ returnDocument: <string>,
45
46
returnNewDocument: <boolean>,
46
47
collation: <document>
47
48
}
48
49
)
49
50
50
- Parameters and Options
51
- ~~~~~~~~~~~~~~~~~~~~~~
51
+ Fields and Options
52
+ ~~~~~~~~~~~~~~~~~~
52
53
53
54
The :method:`~db.collection.findOneAndReplace()` method takes the following
54
- parameters and options:
55
+ fields and options:
55
56
56
57
57
58
.. list-table::
58
59
:header-rows: 1
59
60
:widths: 20 20 80
60
61
61
- * - Parameter
62
-
62
+ * - Field
63
63
- Type
64
-
65
64
- Description
66
65
67
66
* - :ref:`filter <findOneAndReplace-filter>`
68
-
69
67
- document
70
-
71
68
- .. _findOneAndReplace-filter:
72
-
69
+
73
70
The selection criteria for the update. The same :ref:`query
74
71
selectors <query-selectors>` as in the :method:`find()
75
72
<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
+
80
77
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
-
86
78
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.
88
81
82
+ * - :ref:`replacement <findOneAndReplace-replacement>`
89
83
- document
90
-
91
84
- .. _findOneAndReplace-replacement:
92
-
85
+
93
86
The replacement document.
94
-
87
+
95
88
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>`.
102
90
103
- * - :ref:`projection <findOneAndReplace-projection>`
91
+ The ``<replacement>`` document cannot specify an ``_id`` value
92
+ that differs from the replaced document.
104
93
94
+ * - :ref:`projection <findOneAndReplace-projection>`
105
95
- document
106
-
107
96
- .. _findOneAndReplace-projection:
108
-
97
+
109
98
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
-
117
99
118
- * - :ref:`sort <findOneAndReplace-sort>`
100
+ To return all fields in the matching document, omit this
101
+ field.
119
102
120
- - document
103
+ Starting in MongoDB 4.2, the operation returns an error if the
104
+ projection field is not a document.
121
105
106
+ * - :ref:`sort <findOneAndReplace-sort>`
107
+ - document
122
108
- .. _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>`.
125
112
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.
128
115
129
116
See :method:`cursor.sort()`.
130
-
131
-
132
117
133
118
* - ``maxTimeMS``
134
-
135
119
- 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.
141
123
142
124
* - :ref:`upsert <findOneAndReplace-upsert>`
143
-
144
125
- boolean
145
-
146
126
- .. _findOneAndReplace-upsert:
147
127
148
128
.. include:: /includes/extracts/findOneAndReplace-behavior-method.rst
149
-
150
-
151
129
152
- * - :ref:`returnNewDocument <findOneAndReplace-returnNewDocument>`
130
+ * - :ref:`returnDocument <findOneAndReplace-returnDocument>`
131
+ - string
132
+ - .. _findOneAndReplace-returnDocument:
153
133
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.
155
138
139
+ ``returnDocument: "before"`` returns the original document.
140
+ ``returnDocument: "after"`` returns the updated document.
141
+
142
+ * - :ref:`returnNewDocument <findOneAndReplace-returnNewDocument>`
143
+ - boolean
156
144
- .. _findOneAndReplace-returnNewDocument:
157
145
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
+
161
149
Defaults to ``false``.
162
-
163
-
164
150
165
- * - :ref:`collation <findOneAndReplace-collation>`
166
151
152
+ * - :ref:`collation <findOneAndReplace-collation>`
167
153
- document
168
-
169
154
- .. _findOneAndReplace-collation:
170
155
171
156
Optional.
172
-
157
+
173
158
.. include:: /includes/extracts/collation-option.rst
174
-
175
-
176
159
177
160
178
161
Returns
179
162
~~~~~~~
180
163
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
+
184
169
Behavior
185
170
--------
186
171
@@ -189,14 +174,14 @@ Document Match
189
174
190
175
:method:`db.collection.findOneAndReplace()` replaces the first matching
191
176
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.
193
178
194
179
Projection
195
180
~~~~~~~~~~
196
181
197
182
.. include:: /includes/extracts/projection-language-consistency-admonition.rst
198
183
199
- The ``projection`` parameter takes a document in the following form:
184
+ The ``projection`` field takes a document in the following form:
200
185
201
186
.. code-block:: javascript
202
187
@@ -451,17 +436,17 @@ If the operation exceeds the time limit, it returns:
451
436
Replace Document with Upsert
452
437
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
453
438
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>`:
457
442
458
443
.. code-block:: javascript
459
444
460
445
try {
461
446
db.scores.findOneAndReplace(
462
447
{ "team" : "Fortified Lobsters" },
463
448
{ "_id" : 6019, "team" : "Fortified Lobsters" , "score" : 32000},
464
- { upsert : true, returnNewDocument: true }
449
+ { upsert : true, returnDocument: "after" }
465
450
);
466
451
} catch (e){
467
452
print(e);
@@ -477,8 +462,8 @@ The operation returns the following:
477
462
"score" : 32000
478
463
}
479
464
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.
482
467
483
468
Specify Collation
484
469
~~~~~~~~~~~~~~~~~
0 commit comments