@@ -39,145 +39,130 @@ form:
39
39
sort: <document>,
40
40
maxTimeMS: <number>,
41
41
upsert: <boolean>,
42
+ returnDocument: <string>,
42
43
returnNewDocument: <boolean>,
43
44
collation: <document>
44
45
}
45
46
)
46
47
47
- Parameters and Options
48
- ~~~~~~~~~~~~~~~~~~~~~~
48
+ Fields and Options
49
+ ~~~~~~~~~~~~~~~~~~
49
50
50
51
The :method:`~db.collection.findOneAndReplace()` method takes the following
51
- parameters and options:
52
+ fields and options:
52
53
53
54
54
55
.. list-table::
55
56
:header-rows: 1
56
57
:widths: 20 20 80
57
58
58
- * - Parameter
59
-
59
+ * - Field
60
60
- Type
61
-
62
61
- Description
63
62
64
63
* - :ref:`filter <findOneAndReplace-filter>`
65
-
66
64
- document
67
-
68
65
- .. _findOneAndReplace-filter:
69
-
66
+
70
67
The selection criteria for the update. The same :ref:`query
71
68
selectors <query-selectors>` as in the :method:`find()
72
69
<db.collection.find()>` method are available.
73
-
74
- Specify an empty document ``{ }`` to replace the first document returned in
75
- the collection .
76
-
70
+
71
+ To replace the first document returned in the collection,
72
+ specify an empty document ``{ }`` .
73
+
77
74
If unspecified, defaults to an empty document.
78
-
79
- Starting in MongoDB 4.2 (and 4.0.12+, 3.6.14+, and 3.4.23+), the operation
80
- errors if the query argument is not a document.
81
-
82
-
83
75
84
- * - :ref:`replacement <findOneAndReplace-replacement>`
76
+ Starting in MongoDB 4.2, the operation returns an error if the
77
+ query argument is not a document.
85
78
79
+ * - :ref:`replacement <findOneAndReplace-replacement>`
86
80
- document
87
-
88
81
- .. _findOneAndReplace-replacement:
89
-
82
+
90
83
The replacement document.
91
-
84
+
92
85
Cannot contain
93
- :doc:`update operators</reference/operator/update>`.
94
-
95
- The ``<replacement>`` document cannot specify an ``_id`` value that differs
96
- from the replaced document.
97
-
98
-
86
+ :ref:`update operators <update-operators-top-level>`.
99
87
100
- * - :ref:`projection <findOneAndReplace-projection>`
88
+ The ``<replacement>`` document cannot specify an ``_id`` value
89
+ that differs from the replaced document.
101
90
91
+ * - :ref:`projection <findOneAndReplace-projection>`
102
92
- document
103
-
104
93
- .. _findOneAndReplace-projection:
105
-
94
+
106
95
Optional. A subset of fields to return.
107
-
108
- To return all fields in the matching document, omit this parameter.
109
-
110
- Starting in MongoDB 4.2 (and 4.0.12+, 3.6.14+, and 3.4.23+), the operation
111
- errors if the projection argument is not a document.
112
-
113
-
114
96
115
- * - :ref:`sort <findOneAndReplace-sort>`
97
+ To return all fields in the matching document, omit this
98
+ field.
116
99
117
- - document
100
+ Starting in MongoDB 4.2, the operation returns an error if the
101
+ projection field is not a document.
118
102
103
+ * - :ref:`sort <findOneAndReplace-sort>`
104
+ - document
119
105
- .. _findOneAndReplace-sort:
120
-
121
- Optional. Specifies a sorting order for the documents matched by the ``filter``.
106
+
107
+ Optional. Specifies a sorting order for the documents matched by
108
+ the :ref:`filter <findOneAndReplace-filter>`.
122
109
123
- Starting in MongoDB 4.2 (and 4.0.12+, 3.6.14+, and 3.4.23+), the operation
124
- errors if the sort argument is not a document.
110
+ Starting in MongoDB 4.2, the operation returns an error if the
111
+ sort field is not a document.
125
112
126
113
See :method:`cursor.sort()`.
127
-
128
-
129
114
130
115
* - ``maxTimeMS``
131
-
132
116
- number
133
-
134
- - Optional. Specifies a time limit in milliseconds within which the operation must
135
- complete. Throws an error if the limit is exceeded.
136
-
137
-
117
+ - Optional. Specifies a time limit in milliseconds within which
118
+ the operation must complete. Returns an error if the limit is
119
+ exceeded.
138
120
139
121
* - :ref:`upsert <findOneAndReplace-upsert>`
140
-
141
122
- boolean
142
-
143
123
- .. _findOneAndReplace-upsert:
144
124
145
125
.. include:: /includes/extracts/findOneAndReplace-behavior-method.rst
146
-
147
-
148
126
149
- * - :ref:`returnNewDocument <findOneAndReplace-returnNewDocument>`
127
+ * - :ref:`returnDocument <findOneAndReplace-returnDocument>`
128
+ - string
129
+ - .. _findOneAndReplace-returnDocument:
150
130
151
- - boolean
131
+ Optional. Starting in :binary:`mongosh` 0.13.2,
132
+ ``returnDocument`` is an alternative for
133
+ :ref:`returnNewDocument <findOneAndReplace-returnNewDocument>`.
134
+ If both options are set, ``returnDocument`` takes precedence.
152
135
136
+ ``returnDocument: "before"`` returns the original document.
137
+ ``returnDocument: "after"`` returns the updated document.
138
+
139
+ * - :ref:`returnNewDocument <findOneAndReplace-returnNewDocument>`
140
+ - boolean
153
141
- .. _findOneAndReplace-returnNewDocument:
154
142
155
- Optional. When ``true``, returns the replacement document instead of the original
156
- document.
157
-
143
+ Optional. When ``true``, returns the replacement document
144
+ instead of the original document.
145
+
158
146
Defaults to ``false``.
159
-
160
-
161
147
162
- * - :ref:`collation <findOneAndReplace-collation>`
163
148
149
+ * - :ref:`collation <findOneAndReplace-collation>`
164
150
- document
165
-
166
151
- .. _findOneAndReplace-collation:
167
152
168
153
Optional.
169
-
154
+
170
155
.. include:: /includes/extracts/collation-option.rst
171
-
172
-
173
156
174
157
175
158
Returns
176
159
~~~~~~~
177
160
178
- Returns either the original document or, if :ref:`returnNewDocument:
179
- true <findOneAndReplace-returnNewDocument>`, the replacement document.
180
-
161
+ Returns the original document by default. Returns the updated document
162
+ if :ref:`returnDocument <findOneAndReplace-returnDocument>` is set to
163
+ ``after`` or :ref:`returnNewDocument
164
+ <findOneAndReplace-returnNewDocument>` is set to ``true``.
165
+
181
166
Behavior
182
167
--------
183
168
@@ -186,14 +171,14 @@ Document Match
186
171
187
172
:method:`db.collection.findOneAndReplace()` replaces the first matching
188
173
document in the collection that matches the ``filter``.
189
- The ``sort`` parameter can be used to influence which document is modified.
174
+ The ``sort`` field can be used to influence which document is modified.
190
175
191
176
Projection
192
177
~~~~~~~~~~
193
178
194
179
.. include:: /includes/extracts/projection-language-consistency-admonition.rst
195
180
196
- The ``projection`` parameter takes a document in the following form:
181
+ The ``projection`` field takes a document in the following form:
197
182
198
183
.. code-block:: javascript
199
184
@@ -448,17 +433,17 @@ If the operation exceeds the time limit, it returns:
448
433
Replace Document with Upsert
449
434
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
450
435
451
- The following operation uses the :ref:`upsert <findOneAndReplace-upsert>` field to insert the
452
- replacement document if no document matches the :ref:`filter
453
- <findOneAndReplace-filter>`:
436
+ The following operation uses the :ref:`upsert
437
+ <findOneAndReplace-upsert>` field to insert the replacement document if
438
+ no document matches the :ref:`filter <findOneAndReplace-filter>`:
454
439
455
440
.. code-block:: javascript
456
441
457
442
try {
458
443
db.scores.findOneAndReplace(
459
444
{ "team" : "Fortified Lobsters" },
460
445
{ "_id" : 6019, "team" : "Fortified Lobsters" , "score" : 32000},
461
- { upsert : true, returnNewDocument: true }
446
+ { upsert : true, returnDocument: "after" }
462
447
);
463
448
} catch (e){
464
449
print(e);
@@ -474,8 +459,8 @@ The operation returns the following:
474
459
"score" : 32000
475
460
}
476
461
477
- If ``returnNewDocument `` was false , the operation would return ``null`` as
478
- there is no original document to return.
462
+ If ``returnDocument: "before" `` was set , the operation would return
463
+ ``null`` because there is no original document to return.
479
464
480
465
Specify Collation
481
466
~~~~~~~~~~~~~~~~~
0 commit comments