1- ================================= 
1+ ================================
22db.collection.findOneAndDelete()
3- ================================= 
3+ ================================
44
55.. default-domain:: mongodb
66
@@ -27,11 +27,12 @@ Definition
2727   The :method:`~db.collection.findOneAndDelete()` method has the following 
2828   form:
2929
30-    .. code-block:: javascript 
30+    .. code-block:: none 
3131
3232      db.collection.findOneAndDelete(
3333         <filter>,
3434         {
35+            writeConcern: <document>,
3536           projection: <document>,
3637           sort: <document>,
3738           maxTimeMS: <number>,
@@ -48,15 +49,11 @@ Definition
4849      :widths: 20 20 80
4950
5051      * - Parameter
51-    
5252        - Type
53-    
5453        - Description
55-     
54+ 
5655      * - ``filter``
57-    
5856        - document
59-    
6057        - The selection criteria for the deletion. The same :ref:`query
6158          selectors <query-selectors>` as in the :method:`find()
6259          <db.collection.find()>` method are available.
@@ -66,62 +63,56 @@ Definition
6663
6764          If unspecified, defaults to an empty document.
6865
69-           Starting in MongoDB 4.2 (and 4.0.12+, 3.6.14+, and 3.4.23+ ), the operation
66+           Starting in MongoDB 4.2 (and 4.0.12+), the operation
7067          errors if the query argument is not a document.
68+ 
69+       * - ``writeConcern``
70+         - document
71+         - Optional. A document expressing the :doc:`write concern </reference/write-concern>`.
72+           Omit to use the default write concern.
73+ 
74+           .. code-block:: javascript
75+ 
76+             { w: <value>, j: <boolean>, wtimeout: <number> }
7177
78+           See :ref:`Delete A Document Using WriteConcern 
79+           <delete-a-document-using-writeconcern>` for usage.
80+ 
81+           .. include:: /includes/extracts/transactions-operations-write-concern.rst
7282
73-    
7483      * - ``projection``
75-    
7684        - document
77-    
7885        - Optional. A subset of fields to return.
7986
8087          To return all fields in the returned document, omit this parameter.
8188
82-           Starting in MongoDB 4.2 (and 4.0.12+, 3.6.14+, and 3.4.23+ ), the operation
89+           Starting in MongoDB 4.2 (and 4.0.12+), the operation
8390          errors if the projection argument is not a document.
84-           
85-           
86-    
91+ 
8792      * - ``sort``
88-    
8993        - document
90-    
9194        - Optional. Specifies a sorting order for the documents matched by the ``filter``.
9295
93-           Starting in MongoDB 4.2 (and 4.0.12+, 3.6.14+, and 3.4.23+),  the operation 
94-           errors if the sort  argument is not a document.
96+           Starting in MongoDB 4.2 (and 4.0.12+) the operation errors if  the sort  
97+           argument is not a document.
9598
9699          See :method:`cursor.sort()`.
97-           
98-           
99-    
100+ 
100101      * - ``maxTimeMS``
101-    
102102        - number
103-    
104103        - Optional. Specifies a time limit in milliseconds within which the operation must 
105104          complete within. Throws an error if the limit is exceeded.
106-           
107-           
108-    
105+ 
109106      * - ``collation``
110-    
111107        - document
112-    
113108        - Optional. 
114-           
115-           .. include:: /includes/extracts/collation-option.rst
116-           
117-           
118-    
119109
110+           .. include:: /includes/extracts/collation-option.rst
120111
121112   :returns:
122113
123114      Returns the deleted document.
124-        
115+ 
125116Behavior
126117--------
127118
@@ -189,7 +180,7 @@ Transactions
189180.. _findOneAndDelete-examples:
190181
191182Examples
192- ~~~~~~~~ 
183+ -------- 
193184
194185.. _findOneAndDelete-example-replace-document:
195186
@@ -200,12 +191,14 @@ The ``scores`` collection contains documents similar to the following:
200191
201192.. code-block:: javascript
202193
203-    { _id: 6305, name : "A. MacDyver", "assignment" : 5, "points" : 24 },
204-    { _id: 6308, name : "B. Batlock", "assignment" : 3, "points" : 22 },
205-    { _id: 6312, name : "M. Tagnum", "assignment" : 5, "points" : 30 },
206-    { _id: 6319, name : "R. Stiles", "assignment" : 2, "points" : 12 },
207-    { _id: 6322, name : "A. MacDyver", "assignment" : 2, "points" : 14 },
208-    { _id: 6234, name : "R. Stiles", "assignment" : 1, "points" : 10 }
194+     db.scores.insertMany( [
195+       { _id: 6305, name : "A. MacDyver", "assignment" : 5, "points" : 24 },
196+       { _id: 6308, name : "B. Batlock", "assignment" : 3, "points" : 22 },
197+       { _id: 6312, name : "M. Tagnum", "assignment" : 5, "points" : 30 },
198+       { _id: 6319, name : "R. Stiles", "assignment" : 2, "points" : 12 },
199+       { _id: 6322, name : "A. MacDyver", "assignment" : 2, "points" : 14 },
200+       { _id: 6234, name : "R. Stiles", "assignment" : 1, "points" : 10 }
201+    ] )
209202
210203The following operation finds the first document where ``name : M. Tagnum`` 
211204and deletes it:
@@ -222,6 +215,58 @@ The operation returns the *original* document that has been deleted:
222215
223216   { _id: 6312, name: "M. Tagnum", "assignment" : 5, "points" : 30 }
224217
218+ .. _delete-a-document-using-writeconcern:
219+ 
220+ Delete A Document Using WriteConcern
221+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
222+ 
223+ The ``scores`` collection contains documents similar to the following:
224+ 
225+ .. code-block:: javascript
226+ 
227+    db.scores.insertMany( [
228+       { _id: 6305, name : "A. MacDyver", "assignment" : 5, "points" : 24 },
229+       { _id: 6308, name : "B. Batlock", "assignment" : 3, "points" : 22 },
230+       { _id: 6312, name : "M. Tagnum", "assignment" : 5, "points" : 30 },
231+       { _id: 6319, name : "R. Stiles", "assignment" : 2, "points" : 12 },
232+       { _id: 6322, name : "A. MacDyver", "assignment" : 2, "points" : 14 },
233+       { _id: 6234, name : "R. Stiles", "assignment" : 1, "points" : 10 }
234+    ] )
235+ 
236+ The following operation uses a write concern document inside of the 
237+ :method:`db.collection.findOneAndDelete()` method with options:
238+ 
239+ - ``w:1`` to requests acknowledgment that the write operation has 
240+   propagated to the standalone mongod or the primary in a replica set. 
241+ - ``j:true`` to tell the number of MongoDB instances specified in ``w:1``
242+   to have the delete written to on-disk journel.
243+ - ``wtimeout : 1000`` to specify a time limit, in milliseconds, 
244+   for the write concern. ``wtimeout`` is only applicable for ``w`` 
245+   values greater than 1.
246+ 
247+ 
248+ .. code-block:: javascript
249+    :emphasize-lines: 3-9
250+ 
251+    db.scores.findOneAndDelete(
252+       {name: "A. MacDyver"},
253+       { writeConcern: {  
254+             w : 1,
255+             j : true,
256+             wtimeout : 1000
257+          } 
258+       }
259+    )
260+ 
261+ The operation returns the following document:
262+ 
263+ .. code-block:: javascript
264+    :copyable: false
265+ 
266+    { _id: 6305, name: 'A. MacDyver', assignment: 5, points: 24 }
267+ 
268+ The document is deleted with the writeConcern options specified.
269+ 
225270.. _findOneAndDelete-example-sort-and-replace-document:
226271
227272Sort And Delete A Document
@@ -231,12 +276,14 @@ The ``scores`` collection contains documents similar to the following:
231276
232277.. code-block:: javascript
233278
234-    { _id: 6305, name : "A. MacDyver", "assignment" : 5, "points" : 24 },
235-    { _id: 6308, name : "B. Batlock", "assignment" : 3, "points" : 22 },
236-    { _id: 6312, name : "M. Tagnum", "assignment" : 5, "points" : 30 },
237-    { _id: 6319, name : "R. Stiles", "assignment" : 2, "points" : 12 },
238-    { _id: 6322, name : "A. MacDyver", "assignment" : 2, "points" : 14 },
239-    { _id: 6234, name : "R. Stiles", "assignment" : 1, "points" : 10 }
279+    db.scores.insertMany( [
280+       { _id: 6305, name : "A. MacDyver", "assignment" : 5, "points" : 24 },
281+       { _id: 6308, name : "B. Batlock", "assignment" : 3, "points" : 22 },
282+       { _id: 6312, name : "M. Tagnum", "assignment" : 5, "points" : 30 },
283+       { _id: 6319, name : "R. Stiles", "assignment" : 2, "points" : 12 },
284+       { _id: 6322, name : "A. MacDyver", "assignment" : 2, "points" : 14 },
285+       { _id: 6234, name : "R. Stiles", "assignment" : 1, "points" : 10 }
286+    ] )
240287
241288The following operation first finds all documents where 
242289``name : "A. MacDyver"``. It then sorts by ``points`` ascending before 
@@ -289,18 +336,22 @@ The following operation sets a 5ms time limit to complete the deletion:
289336   try {
290337      db.scores.findOneAndDelete(
291338         { "name" : "A. MacDyver" },
292-          { sort : { "points" : 1 }, maxTimeMS : 5 }; 
293-       ); 
339+          { sort : { "points" : 1 }, maxTimeMS : 5 }
340+       )
294341   }
295342   catch(e){
296-       print(e); 
343+       print(e)
297344   }
298345
299346If the operation exceeds the time limit, it returns:
300347
301348.. code-block:: javascript
349+    
350+    MongoServerError: operation exceeded time limit: { "ok": 0, "code" : 50, "codeName" : "MaxTimeMSExpired" }
351+ 
352+ .. note::
302353
303-    Error: findAndModifyFailed failed: { "ok" : 0, "errmsg" : "operation exceeded time limit", "code" : 50 } 
354+    This error message has been shortened for brevity. 
304355
305356Specify Collation
306357~~~~~~~~~~~~~~~~~
@@ -311,9 +362,11 @@ A collection ``myColl`` has the following documents:
311362
312363.. code-block:: javascript
313364
314-    { _id: 1, category: "café", status: "A" }
315-    { _id: 2, category: "cafe", status: "a" }
316-    { _id: 3, category: "cafE", status: "a" }
365+    db.myColl.insertMany( [
366+       { _id: 1, category: "café", status: "A" },
367+       { _id: 2, category: "cafe", status: "a" },
368+       { _id: 3, category: "cafE", status: "a" }
369+    ] )
317370
318371The following operation includes the :ref:`collation <collation>`
319372option:
0 commit comments