Skip to content

Commit a122ff1

Browse files
committed
DOCS-11040: db.collection.watch()
1 parent e1454e2 commit a122ff1

8 files changed

+451
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
name: resumeAfter
2+
position: 1
3+
description: |
4+
5+
Directs :method:`~db.collection.watch` to attempt resuming
6+
notifications starting after the operation specified in the resume
7+
token.
8+
9+
Each change stream event document includes a resume token as the
10+
``_id`` field. Pass the *entire* ``_id`` field of the change event
11+
document that represents the operation you want to resume after.
12+
13+
optional: true
14+
type: document
15+
operation: db.collection.watch
16+
interface: method
17+
arg_name: field
18+
---
19+
name: fullDocument
20+
position: 2
21+
description: |
22+
23+
By default, :method:`~db.collection.watch()` returns the delta of
24+
those fields modified by an update operation, instead of the entire
25+
updated document.
26+
27+
Set ``fullDocument`` to ``"updateLookup"`` to direct
28+
:method:`~db.collection.watch()` to look up the most current
29+
majority-committed version of the updated document.
30+
:method:`~db.collection.watch()` returns a ``fullDocument`` field with
31+
the document lookup in addition to the ``updateDescription`` delta.
32+
33+
optional: true
34+
type: string
35+
operation: db.collection.watch
36+
interface: method
37+
arg_name: field
38+
---
39+
name: batchSize
40+
position: 3
41+
description: |
42+
Specifies the maximum number of change events to return in each
43+
batch of the response from the MongoDB cluster.
44+
45+
Has the same functionality as :method:`cursor.batchSize()`.
46+
optional: true
47+
type: int
48+
operation: db.collection.watch
49+
interface: method
50+
arg_name: field
51+
---
52+
name: maxAwaitTimeMS
53+
position: 4
54+
description: |
55+
56+
The maximum amount of time in milliseconds the server waits for new
57+
data changes to report to the change stream cursor before returning an
58+
empty batch.
59+
60+
Defaults to ``1000`` milliseconds.
61+
62+
optional: true
63+
type: int
64+
operation: db.collection.watch
65+
interface: method
66+
arg_name: field
67+
---
68+
name: collation
69+
position: 5
70+
description: |
71+
Pass a :ref:`collation document <collation-document-fields>`
72+
to specify a :doc:`collation </reference/collation>` for the
73+
change stream cursor.
74+
optional: true
75+
type: document
76+
operation: db.collection.watch
77+
interface: method
78+
arg_name: field
79+
...
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
arg_name: param
2+
description: |
3+
4+
A sequence of one or more of the following aggregation stages:
5+
6+
- :pipeline:`$match`
7+
- :pipeline:`$project`
8+
- :pipeline:`$addFields`
9+
- :pipeline:`$replaceRoot`
10+
- :pipeline:`$redact`
11+
12+
See :doc:`/aggregation` for complete documentation on the aggregation
13+
framework.
14+
15+
interface: method
16+
name: pipeline
17+
operation: db.collection.watch
18+
optional: false
19+
position: 1
20+
type: array
21+
---
22+
arg_name: param
23+
description: |
24+
25+
Additional options that modify the behavior of
26+
:method:`~db.collection.watch()`.
27+
28+
You must pass an empty array ``[]`` to the ``pipeline``
29+
parameter if you are not specifying a pipeline but are
30+
are passing the ``options`` document.
31+
32+
interface: method
33+
name: options
34+
operation: db.collection.watch
35+
optional: true
36+
position: 2
37+
type: document
38+
...

source/includes/ref-toc-method-collection.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ name: ":method:`db.collection.updateMany()`"
170170
file: /reference/method/db.collection.updateMany
171171
description: "Modifies multiple documents in a collection."
172172
---
173+
name: ":method:`db.collection.watch()`"
174+
file: /reference/method/db.collection.watch
175+
description: "Establishes a Change Stream on a collection."
176+
---
173177
name: ":method:`db.collection.validate()`"
174178
file: /reference/method/db.collection.validate
175179
description: "Performs diagnostic operations on a collection."

source/includes/ref-toc-method-cursor.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ name: ":method:`cursor.close()`"
1010
file: /reference/method/cursor.close
1111
description: "Close a cursor and free associated server resources."
1212
---
13+
name: ":method:`cursor.isClosed()`"
14+
file: /reference/method/cursor.isClosed
15+
description: "Returns ``true`` if the cursor is closed."
16+
---
1317
name: ":method:`cursor.collation()`"
1418
file: /reference/method/cursor.collation
1519
description: "Specifies the collation for the cursor returned by the :method:`db.collection.find()`."
@@ -38,6 +42,10 @@ name: ":method:`cursor.hint()`"
3842
file: /reference/method/cursor.hint
3943
description: "Forces MongoDB to use a specific index for a query."
4044
---
45+
name: ":method:`cursor.isExhausted()`"
46+
file: /reference/method/cursor.isExhausted
47+
description: "Returns ``true`` if the cursor is closed *and* there are no objects remaining in the batch."
48+
---
4149
name: ":method:`cursor.itcount()`"
4250
file: /reference/method/cursor.itcount
4351
description: "Computes the total number of documents in the cursor client-side by fetching and iterating the result set."

source/reference/change-events.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ with a shard key on ``userName`` and ``_id``.
155155
The ``fullDocument`` document represents the version of the document at the
156156
time of the insert.
157157

158+
.. _change-streams-update-event:
159+
158160
``update`` Event
159161
----------------
160162

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
=================
2+
cursor.isClosed()
3+
=================
4+
5+
.. default-domain:: mongodb
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 1
11+
:class: singlecol
12+
13+
.. method:: cursor.isClosed()
14+
15+
:returns: Boolean.
16+
17+
:method:`cursor.isClosed()` returns ``true`` if the server has
18+
closed the cursor.
19+
20+
A closed cursor may still have documents remaining in the last
21+
received batch. Use :method:`cursor.isExhausted()` or
22+
:method:`cursor.hasNext()` to check if the cursor is fully
23+
exhausted.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
====================
2+
cursor.isExhausted()
3+
====================
4+
5+
.. default-domain:: mongodb
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 1
11+
:class: singlecol
12+
13+
.. method:: cursor.isExhausted()
14+
15+
:returns: Boolean.
16+
17+
:method:`cursor.isExhausted()` returns ``true`` if the cursor
18+
is closed *and* there are no remaining objects in the batch.
19+
20+
Use :method:`~cursor.isExhausted()` to support iterating cursors
21+
that remain open even if there are no documents remaining in the
22+
current batch, such as :method:`tailable <cursor.tailable()>` or
23+
:method:`change stream <db.collection.watch()>` cursors.
24+
25+
Example
26+
-------
27+
28+
Consider the following ``while`` loop iterating through updates to
29+
a :method:`change stream <db.collection.watch()>` cursor:
30+
31+
.. code-block:: javascript
32+
33+
watchCursor = db.collection.watch();
34+
35+
while (watchCursor.hasNext()) {
36+
watchCursor.next();
37+
}
38+
39+
A change stream cursor can return an empty batch if no new data changes
40+
have occured within a set period of time. This causes the while loop
41+
to exit prematurely as :method:`cursor.hasNext()` returns ``false``
42+
when it detects the empty batch. However, the change stream cursor
43+
is still open and able to return more documents in the future.
44+
45+
Use :method:`cursor.isExhausted()` to ensure the while loop only exits
46+
when the cursor is closed *and* there are no documents remaining in the
47+
batch:
48+
49+
.. code-block:: javascript
50+
51+
watchCursor = db.collection.watch();
52+
53+
while (!watchCursor.isExhausted()) {
54+
if (watchCursor.hasNext()){
55+
watchCursor.next();
56+
}
57+
}

0 commit comments

Comments
 (0)