===============================
MongoDB\\ChangeStream::rewind()
===============================

.. default-domain:: mongodb

.. contents:: On this page
   :local:
   :backlinks: none
   :depth: 1
   :class: singlecol

Definition
----------

.. phpmethod:: MongoDB\\ChangeStream::rewind()

   Rewinds the change stream and attempts to load the first event.

   .. code-block:: php

      function rewind(): void

   This method should be called at the start of change stream iteration.

   .. note::

      Rewinding the change stream does not guarantee that there will be a
      current event to access. You should still call
      :phpmethod:`MongoDB\\ChangeStream::valid()` to check for a current event
      at each step of iteration. After initially rewinding the change stream,
      :phpmethod:`MongoDB\\ChangeStream::next()` should be used to iterate
      further.

Errors/Exceptions
-----------------

:php:`MongoDB\\Driver\\Exception\\LogicException
<mongodb-driver-exception-logicexception>` if this method is called after a call
to :phpmethod:`MongoDB\\ChangeStream::next()` (i.e. the underlying
:php:`MongoDB\\Driver\\Cursor <class.mongodb-driver-cursor>` has already been
advanced).

.. include:: /includes/extracts/error-driver-runtimeexception.rst

See Also
--------

- :phpmethod:`MongoDB\\Collection::watch()`
- :php:`Iterator::rewind() <iterator.rewind>`
- :ref:`Tailable Cursor Iteration <php-tailable-cursor>`
- :manual:`Change Streams </changeStreams>` documentation in the MongoDB manual
