Advance the cursor. This method blocks until the next change document is returned or an unrecoverable error is raised. This method is used when iterating over all changes in the cursor. For example:: try: resume_token = None pipel
(self)
| 285 | |
| 286 | @_csot.apply |
| 287 | def next(self) -> _DocumentType: |
| 288 | """Advance the cursor. |
| 289 | |
| 290 | This method blocks until the next change document is returned or an |
| 291 | unrecoverable error is raised. This method is used when iterating over |
| 292 | all changes in the cursor. For example:: |
| 293 | |
| 294 | try: |
| 295 | resume_token = None |
| 296 | pipeline = [{'$match': {'operationType': 'insert'}}] |
| 297 | with db.collection.watch(pipeline) as stream: |
| 298 | for insert_change in stream: |
| 299 | print(insert_change) |
| 300 | resume_token = stream.resume_token |
| 301 | except pymongo.errors.PyMongoError: |
| 302 | # The ChangeStream encountered an unrecoverable error or the |
| 303 | # resume attempt failed to recreate the cursor. |
| 304 | if resume_token is None: |
| 305 | # There is no usable resume token because there was a |
| 306 | # failure during ChangeStream initialization. |
| 307 | logging.error('...') |
| 308 | else: |
| 309 | # Use the interrupted ChangeStream's resume token to create |
| 310 | # a new ChangeStream. The new stream will continue from the |
| 311 | # last seen insert change without missing any events. |
| 312 | with db.collection.watch( |
| 313 | pipeline, resume_after=resume_token) as stream: |
| 314 | for insert_change in stream: |
| 315 | print(insert_change) |
| 316 | |
| 317 | Raises :exc:`StopIteration` if this ChangeStream is closed. |
| 318 | """ |
| 319 | while self.alive: |
| 320 | doc = self.try_next() |
| 321 | if doc is not None: |
| 322 | return doc |
| 323 | |
| 324 | raise StopIteration |
| 325 | |
| 326 | __next__ = next |
| 327 |