2
2
Compound Operations
3
3
===================
4
4
5
- .. default-domain:: mongodb
6
-
7
5
.. contents:: On this page
8
6
:local:
9
7
:backlinks: none
@@ -13,52 +11,66 @@ Compound Operations
13
11
Overview
14
12
--------
15
13
16
- Most database requests only need to read data out of a database or
17
- write data into a database. However, client applications sometimes need
18
- to read and write data in a single interaction with the database .
14
+ Most database requests need to either read data out of a database or
15
+ write data into a database. However, there are instances where you may
16
+ need to read and write data in a single interaction.
19
17
20
- Compound operations combine read and write operations
18
+ ** Compound operations** combine read and write operations
21
19
in a single atomic statement, so there's no chance of data changing in
22
- between a read and a subsequent write; in fact, both operations take
23
- place in the same line of code from the perspective of your client
24
- application.
25
-
26
- This property can be useful in cases where you want to write to a
27
- specific document, but you haven't found it yet. If you just perform a
28
- read for the document's ``_id`` and then try to alter the document you
29
- just found, it's possible that someone else can alter the document in
30
- between your read and write operations. This doesn't stop you from doing
31
- this work, but it can make error handling much more difficult. Compound
32
- operations help keep your logic straightforward by handling that logic
33
- entirely inside the database behind a layer of abstraction, so you don't
34
- have to worry about it. While you can accomplish this task using
35
- separate reads and writes, doing so requires the client application to
36
- gracefully handle potential errors at any stage of the process and in
37
- multiple potential error states. This increases the complexity of your
38
- code and can make your client application brittle and difficult to test.
20
+ between a read and a subsequent write.
21
+
22
+ If you execute each operation separately, another request may alter the
23
+ data between the read and write operations. These data changes may not
24
+ prevent your operation from succeeding, but they can make error handling
25
+ more difficult. When your application has to handle potential errors at
26
+ any stage of the process, it can become brittle and difficult
27
+ to test.
39
28
40
29
Built-in Methods
41
30
----------------
42
31
43
- There are three major compound operations:
32
+ The {+driver-short+} provides the following methods to perform compound
33
+ operations:
44
34
45
35
- `findOneAndDelete() <{+api+}/classes/Collection.html#findOneAndDelete>`__
46
- matches multiple documents to a supplied query and removes the first
47
- of those matched documents.
48
-
49
36
- `findOneAndUpdate() <{+api+}/classes/Collection.html#findOneAndUpdate>`__
50
- matches multiple documents to a supplied query and updates the first
51
- of those matched documents using the provided update document.
52
-
53
37
- `findOneAndReplace() <{+api+}/classes/Collection.html#findOneAndReplace>`__
54
- matches multiple documents to a supplied query and replaces the first
55
- of those matched documents using the provided replacement document.
56
-
57
- All three methods accept an optional ``options`` object with
58
- configurable :doc:`sort </fundamentals/crud/read-operations/sort>` and
59
- :doc:`projection </fundamentals/crud/read-operations/project>` options
60
- that work just like their read operation equivalents.
61
- ``findOneAndUpdate()`` and ``findOneAndDelete()`` allow the client to
62
- configure the ``returnDocument`` option, a boolean that determines if
63
- the method returns the pre-update or post-update version of the modified
64
- document.
38
+
39
+ These methods accept an optional ``options`` object with
40
+ configurable :ref:`sort <node-fundamentals-sort>` and
41
+ :ref:`projection <node-fundamentals-project>` options.
42
+
43
+ .. note:: includeResultMetadata Option
44
+
45
+ Starting in version 5.7, you can set the ``includeResultMetadata``
46
+ setting in the ``options`` object to specify the return type for each
47
+ of these methods. The setting defaults to ``true``, which means the
48
+ method returns a ``ModifyResult`` type. If you set
49
+ ``includeResultMetadata`` to ``false``, the method returns the
50
+ modified document.
51
+
52
+ Suppose a collection contains the following document:
53
+
54
+ .. code-block:: json
55
+
56
+ { _id: 1, x: "on" }
57
+
58
+ The following code shows the return type for ``findOneAndDelete()``
59
+ operations when ``includeResultMetadata`` is set to ``true`` and
60
+ ``false``:
61
+
62
+ .. code-block:: js
63
+
64
+ // returns { _id: 1, x: 'on' }
65
+ await coll.findOneAndDelete({ x: "on" }, { includeResultMetadata: false });
66
+
67
+ // returns { lastErrorObject: { n: 1 }, value: { _id: 1, x: 'on' }, ok: 1, ... }
68
+ await coll.findOneAndDelete({ x: "on" }, { includeResultMetadata: true });
69
+
70
+ // no document matched, returns null
71
+ await coll.findOneAndDelete({ x: "off" }, { includeResultMetadata: false });
72
+
73
+ You can set the ``returnDocument`` setting in the ``options`` object for the
74
+ ``findOneAndUpdate()`` and ``findOneAndDelete()`` methods, which lets
75
+ you specify if the method returns the pre-update or post-update version
76
+ of the modified document.
0 commit comments