|
| 1 | +========================== |
| 2 | +$tsIncrement (aggregation) |
| 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 | +Definition |
| 14 | +---------- |
| 15 | + |
| 16 | +.. expression:: $tsIncrement |
| 17 | + |
| 18 | +.. versionadded:: 5.1 |
| 19 | + |
| 20 | +.. include:: /includes/tsIncrement-introduction.rst |
| 21 | + |
| 22 | +When multiple events happen within the same second, the incrementing |
| 23 | +ordinal uniquely identifies each event. |
| 24 | + |
| 25 | +:expression:`$tsIncrement` syntax: |
| 26 | + |
| 27 | +.. code-block:: none |
| 28 | + :copyable: false |
| 29 | + |
| 30 | + { $tsIncrement: <expression> } |
| 31 | + |
| 32 | +The :ref:`expression <aggregation-expressions>` must resolve to a |
| 33 | +:ref:`timestamp <document-bson-type-timestamp>`. |
| 34 | + |
| 35 | +.. seealso:: |
| 36 | + |
| 37 | + - :ref:`aggregation-expressions` |
| 38 | + - :ref:`bson-types` |
| 39 | + - :expression:`$tsSecond` |
| 40 | + |
| 41 | +Behavior |
| 42 | +-------- |
| 43 | + |
| 44 | +:expression:`$tsIncrement` returns: |
| 45 | + |
| 46 | +- ``Null`` if the input :ref:`expression <aggregation-expressions>` |
| 47 | + evaluates to ``null`` or refers to a field that is missing. |
| 48 | + |
| 49 | +- An error if the input :ref:`expression <aggregation-expressions>` does |
| 50 | + not evaluate to a :ref:`timestamp <document-bson-type-timestamp>`. |
| 51 | + |
| 52 | +Examples |
| 53 | +-------- |
| 54 | + |
| 55 | +Obtain the Incrementing Ordinal from a Timestamp Field |
| 56 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 57 | + |
| 58 | +.. include:: /includes/stockSales-example-collection.rst |
| 59 | + |
| 60 | +The following example uses :expression:`$tsIncrement` in a |
| 61 | +:pipeline:`$project` stage to return the incrementing ordinal from the |
| 62 | +stock sales ``saleTimestamp`` field: |
| 63 | + |
| 64 | +.. code-block:: javascript |
| 65 | + |
| 66 | + db.stockSales.aggregate( [ |
| 67 | + { |
| 68 | + $project: |
| 69 | + { |
| 70 | + _id: 0, saleTimestamp: 1, saleIncrement: { $tsIncrement: "$saleTimestamp" } |
| 71 | + } |
| 72 | + } |
| 73 | + ] ) |
| 74 | + |
| 75 | +In the example, :pipeline:`$project` only includes the ``saleTimestamp`` |
| 76 | +and ``saleIncrement`` fields as shown in the following output: |
| 77 | + |
| 78 | +.. code-block:: javascript |
| 79 | + :copyable: false |
| 80 | + |
| 81 | + { |
| 82 | + saleTimestamp: Timestamp({ t: 1622731060, i: 1 }), |
| 83 | + saleIncrement: Long("1") |
| 84 | + }, |
| 85 | + { |
| 86 | + saleTimestamp: Timestamp({ t: 1622731060, i: 2 }), |
| 87 | + saleIncrement: Long("2") |
| 88 | + }, |
| 89 | + { |
| 90 | + saleTimestamp: Timestamp({ t: 1714124193, i: 1 }), |
| 91 | + saleIncrement: Long("1") |
| 92 | + }, |
| 93 | + { |
| 94 | + saleTimestamp: Timestamp({ t: 1714124193, i: 2 }), |
| 95 | + saleIncrement: Long("2") |
| 96 | + }, |
| 97 | + { |
| 98 | + saleTimestamp: Timestamp({ t: 1714124193, i: 3 }), |
| 99 | + saleIncrement: Long("3") |
| 100 | + } |
| 101 | + |
| 102 | +Use ``$tsIncrement`` in a Change Stream Cursor to Monitor Collection Changes |
| 103 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 104 | + |
| 105 | +The example in this section uses :expression:`$tsIncrement` in a |
| 106 | +:ref:`change stream cursor <changeStreams>` to return every other change |
| 107 | +made to a collection in the same second of time. |
| 108 | + |
| 109 | +Create a :ref:`change stream cursor <changeStreams>` on a collection |
| 110 | +named ``cakeSales`` that you will see later in this section: |
| 111 | + |
| 112 | +.. code-block:: javascript |
| 113 | + |
| 114 | + cakeSalesCursor = db.cakeSales.watch( [ |
| 115 | + { |
| 116 | + $match: { |
| 117 | + $expr: { |
| 118 | + $eq: [ |
| 119 | + { $mod: [ { $tsIncrement: "$clusterTime" } , 2 ] }, |
| 120 | + 0 |
| 121 | + ] |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + ] ) |
| 126 | + |
| 127 | +In the example, the: |
| 128 | + |
| 129 | +- :method:`db.collection.watch()` method creates a :ref:`change stream |
| 130 | + cursor <changeStreams>` for the ``cakeSales`` collection and stores |
| 131 | + the cursor in ``cakeSalesCursor``. |
| 132 | + |
| 133 | +- :pipeline:`$match` stage filters the documents to those |
| 134 | + returned by the :query:`$expr` operator. |
| 135 | + |
| 136 | +- :query:`$expr` operator: |
| 137 | + |
| 138 | + - Applies :expression:`$mod` ``2`` to the ``$clusterTime`` variable's |
| 139 | + incrementing ordinal returned by :expression:`$tsIncrement`. |
| 140 | + |
| 141 | + ``$clusterTime`` is the timestamp from the :ref:`oplog |
| 142 | + <replica-set-oplog>` entry when the ``cakeSales`` collection is |
| 143 | + modified. See :ref:`Command Response <command-response>`. |
| 144 | + |
| 145 | + - Compares the returned value from :expression:`$mod` to ``0`` using |
| 146 | + :expression:`$eq`. |
| 147 | + |
| 148 | +.. include:: /includes/cakeSales-example-collection.rst |
| 149 | + |
| 150 | +To monitor the ``cakeSales`` collection changes, use |
| 151 | +``cakeSalesCursor``. For example, to obtain the next document from |
| 152 | +``cakeSalesCursor``, use the :method:`~cursor.next()` method: |
| 153 | + |
| 154 | +.. code-block:: javascript |
| 155 | + |
| 156 | + cakeSalesCursor.next() |
| 157 | + |
| 158 | +Depending on the second when the documents were added to ``cakeSales``, |
| 159 | +the output from ``cakeSalesCursor.next()`` varies. For example, the |
| 160 | +document additions might span more than one second. |
| 161 | + |
| 162 | +The following ``cakeSalesCursor.next()`` example output shows the |
| 163 | +``insert`` details for the first document added to the ``cakeSales`` |
| 164 | +collection. Notice the incrementing ordinal ``i`` is ``2`` in the |
| 165 | +``clusterTime`` field. |
| 166 | + |
| 167 | +.. code-block:: javascript |
| 168 | + :copyable: false |
| 169 | + |
| 170 | + _id: { |
| 171 | + _data: '82613A4F25000000022B022C0100296E5A100454C5BFAF538C47AB950614F43889BE00461E5F696400290004' |
| 172 | + }, |
| 173 | + operationType: 'insert', |
| 174 | + clusterTime: Timestamp({ t: 1631211301, i: 2 }), |
| 175 | + fullDocument: { |
| 176 | + _id: 0, |
| 177 | + type: 'chocolate', |
| 178 | + orderDate: ISODate("2020-05-18T14:10:30.000Z"), |
| 179 | + state: 'CA', |
| 180 | + price: 13, |
| 181 | + quantity: 120 |
| 182 | + }, |
| 183 | + ns: { db: 'test', coll: 'cakeSales' }, |
| 184 | + documentKey: { _id: 0 } |
| 185 | + |
| 186 | +Running ``cakeSalesCursor.next()`` again returns the ``cakeSales`` |
| 187 | +document for which the ``clusterTime`` incrementing ordinal ``i`` is |
| 188 | +``4``, omitting the document where ``i`` is ``3``. |
0 commit comments