Skip to content

Commit cc2f138

Browse files
jason-price-mongodbjason-price-mongodb
andauthored
DOCSP-18759 pre post images (#941)
* DOCSP-18759-PIT-pre-post-images * DOCSP-18759-PIT-pre-post-images * DOCSP-18759-PIT-pre-post-images * DOCSP-18759-PIT-pre-post-images * DOCSP-18759-PIT-pre-post-images * DOCSP-18759-PIT-pre-post-images * DOCSP-18759-PIT-pre-post-images * DOCSP-18759-PIT-pre-post-images * DOCSP-18759-PIT-pre-post-images * DOCSP-18759-PIT-pre-post-images * DOCSP-18759-PIT-pre-post-images * DOCSP-18759-PIT-pre-post-images * DOCSP-18759-PIT-pre-post-images * DOCSP-18759-PIT-pre-post-images * DOCSP-18759-PIT-pre-post-images * DOCSP-18759-PIT-pre-post-images * DOCSP-18759-PIT-pre-post-images * DOCSP-18759-PIT-pre-post-images * DOCSP-18759-PIT-pre-post-images * DOCSP-18759-PIT-pre-post-images * DOCSP-18759-PIT-pre-post-images * DOCSP-18759-PIT-pre-post-images * DOCSP-18759-PIT-pre-post-images * DOCSP-18759-PIT-pre-post-images * DOCSP-18759-PIT-pre-post-images * DOCSP-18759-PIT-pre-post-images * DOCSP-18759-PIT-pre-post-images * DOCSP-18759-PIT-pre-post-images * DOCSP-18759-PIT-pre-post-images * DOCSP-18759-PIT-pre-post-images * DOCSP-18759-PIT-pre-post-images * DOCSP-18759-PIT-pre-post-images Co-authored-by: jason-price-mongodb <[email protected]>
1 parent 65b093e commit cc2f138

18 files changed

+568
-9
lines changed

source/changeStreams.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,16 @@ Change Streams and Orphan Documents
10501050

10511051
.. include:: /includes/change-streams-and-orphans.rst
10521052

1053+
Change Streams with Document Pre- and Post-Images
1054+
-------------------------------------------------
1055+
1056+
.. include:: /includes/change-stream-pre-and-post-images-introduction.rst
1057+
1058+
.. include:: /includes/change-stream-pre-and-post-images-additional-information.rst
1059+
1060+
For complete examples with the change stream output, see
1061+
:ref:`db.collection.watch-change-streams-pre-and-post-images-example`.
1062+
10531063
.. toctree::
10541064
:titlesonly:
10551065
:hidden:
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
Pre- and post-images are not available for a :ref:`change stream event
2+
<change-stream-output>` if the images were:
3+
4+
- Not enabled on the collection at the time of a document update or
5+
delete operation.
6+
7+
- Removed after the pre- and post-image retention time set in
8+
``expireAfterSeconds``.
9+
10+
- The following example sets ``expireAfterSeconds`` to ``100``
11+
seconds:
12+
13+
.. code-block:: javascript
14+
15+
use admin
16+
db.runCommand( {
17+
setClusterParameter:
18+
{ changeStreamOptions: { preAndPostImages: { expireAfterSeconds: 100 } } }
19+
} )
20+
21+
- The following example returns the current ``changeStreamOptions``
22+
settings, including ``expireAfterSeconds``:
23+
24+
.. code-block:: javascript
25+
26+
db.adminCommand( { getClusterParameter: "changeStreamOptions" } )
27+
28+
- Setting ``expireAfterSeconds`` to ``off`` uses the default retention
29+
policy: pre- and post-images are retained until the corresponding
30+
change stream events are removed from the :term:`oplog`.
31+
32+
- If a change stream event is removed from the oplog, then the
33+
corresponding pre- and post-images are also deleted regardless of
34+
the ``expireAfterSeconds`` pre- and post-image retention time.
35+
36+
Additional considerations:
37+
38+
- Enabling pre- and post-images consumes storage space and adds
39+
processing time. Only enable pre- and post-images if you need them.
40+
41+
- Limit the change stream event size to less than 16 megabytes. To limit
42+
the event size, you can:
43+
44+
- Limit the document size to 8 megabytes. You can request pre- and
45+
post-images simultaneously in the :ref:`change stream output
46+
<change-stream-output>` if other change stream event fields like
47+
``updateDescription`` are not large.
48+
49+
- Request only post-images in the change stream output for documents
50+
up to 16 megabytes if other change stream event fields like
51+
``updateDescription`` are not large.
52+
53+
- Request only pre-images in the change stream output for documents up
54+
to 16 megabytes if:
55+
56+
- document updates affect only a small fraction of the document
57+
structure or content, *and*
58+
59+
- do not cause a ``replace`` change event. A ``replace`` event
60+
always includes the post-image.
61+
62+
- To request a pre-image, you set ``fullDocumentBeforeChange`` to
63+
``required`` or ``whenAvailable`` in :method:`db.collection.watch()`.
64+
To request a post-image, you set ``fullDocument`` using the same
65+
method.
66+
67+
- Pre-images are written to the ``config.system.preimages`` collection.
68+
69+
- The ``config.system.preimages`` collection may become large. To
70+
limit the collection size, you can set ``expireAfterSeconds``
71+
time for the pre-images as shown earlier.
72+
73+
- Pre-images are removed asynchronously by a background process.
74+
75+
- If you need to downgrade MongoDB to a version that does not support
76+
pre- and post-images, you must first disable pre- and post-images for
77+
the collections.
78+
79+
.. seealso::
80+
81+
- For change stream events and output, see
82+
:ref:`change-stream-output`.
83+
84+
- To watch a collection for changes, see
85+
:method:`db.collection.watch`.
86+
87+
- For complete examples with the change stream output, see
88+
:ref:`db.collection.watch-change-streams-pre-and-post-images-example`.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Starting in MongoDB 6.0, you see a ``fullDocumentBeforeChange``
2+
document with the fields before the document was changed (or deleted)
3+
if you perform these steps:
4+
5+
#. Enable the new ``changeStreamPreAndPostImages`` field for a
6+
collection using :method:`db.createCollection()`,
7+
:dbcommand:`create`, or :dbcommand:`collMod`.
8+
9+
#. Set ``fullDocumentBeforeChange`` to ``"required"`` or
10+
``"whenAvailable"`` in :method:`db.collection.watch()`.
11+
12+
Example ``fullDocumentBeforeChange`` document in the change stream
13+
output:
14+
15+
.. code-block:: javascript
16+
:copyable: false
17+
18+
"fullDocumentBeforeChange" : {
19+
"_id" : ObjectId("599af247bb69cd89961c986d"),
20+
"userName" : "alice123",
21+
"name" : "Alice Smith"
22+
}
23+
24+
For complete examples with the change stream output, see
25+
:ref:`db.collection.watch-change-streams-pre-and-post-images-example`.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
In the example:
2+
3+
- The ``while`` loop runs until the cursor is closed.
4+
5+
- :method:`~cursor.hasNext()` returns ``true`` if the cursor has
6+
documents.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Optional.
2+
3+
.. include:: /includes/change-stream-pre-and-post-images-introduction.rst
4+
5+
``changeStreamPreAndPostImages`` has the following syntax:
6+
7+
.. code-block:: javascript
8+
:copyable: false
9+
10+
changeStreamPreAndPostImages: {
11+
enabled: <boolean>
12+
}
13+
14+
.. list-table::
15+
:header-rows: 1
16+
17+
* - ``enabled``
18+
- Description
19+
20+
* - ``true``
21+
- Enables change stream pre- and post-images for a collection.
22+
23+
* - ``false``
24+
- Disables change stream pre- and post-images for a collection.
25+
26+
For complete examples with the change stream output, see
27+
:ref:`db.collection.watch-change-streams-pre-and-post-images-example`.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Starting in MongoDB 6.0, you can use the new
2+
``fullDocumentBeforeChange`` field and set it to:
3+
4+
- ``"whenAvailable"`` to output the document pre-image, if available,
5+
before the document was replaced, updated, or deleted.
6+
7+
- ``"required"`` to output the document pre-image before the document
8+
was replaced, updated, or deleted. Raises an error if the pre-image
9+
is not available.
10+
11+
- ``"off"`` to suppress the document pre-image. ``"off"`` is the
12+
default.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Starting in MongoDB 6.0, you can set ``fullDocument`` to:
2+
3+
- ``"whenAvailable"`` to output the document post-image, if available,
4+
after the document was inserted, replaced, or updated.
5+
6+
- ``"required"`` to output the document post-image after the document
7+
was inserted, replaced, or updated. Raises an error if the post-image
8+
is not available.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Starting in MongoDB 6.0, you can use :ref:`change stream events
2+
<change-stream-output>` to output the version of a document before and
3+
after changes (the document pre- and post-images):
4+
5+
- The pre-image is the document before it was replaced, updated, or
6+
deleted. There is no pre-image for an inserted document.
7+
8+
- The post-image is the document after it was inserted, replaced, or
9+
updated. There is no post-image for a deleted document.
10+
11+
- Enable ``changeStreamPreAndPostImages`` for a collection using
12+
:method:`db.createCollection()`, :dbcommand:`create`, or
13+
:dbcommand:`collMod`.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.. seealso::
2+
3+
- For document update output details, see :ref:`change
4+
stream update events <change-streams-update-event>`.
5+
6+
- For change stream output details, see :ref:`change-stream-output`.

source/reference/built-in-roles.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ Cluster Administration Roles
258258
- :authaction:`setDefaultRWConcern` (New in version 4.4)
259259
- :authaction:`setFeatureCompatibilityVersion`
260260
- :authaction:`setFreeMonitoring`
261-
261+
262262
* - *All* :ref:`databases <resource-specific-db>`
263263

264264
- .. hlist::
@@ -832,6 +832,12 @@ The following role provides full privileges on all resources:
832832
The :authrole:`root` role includes privileges from the
833833
:authrole:`backup` and :authrole:`restore` roles.
834834

835+
.. versionchanged:: 6.0
836+
837+
The :authrole:`root` role includes :authaction:`find` and
838+
:authaction:`remove` privileges on the ``system.preimages``
839+
collection in the ``config`` database.
840+
835841
Internal Role
836842
-------------
837843

0 commit comments

Comments
 (0)