|
| 1 | +.. _pymongo-cursors: |
| 2 | + |
| 3 | +========================= |
| 4 | +Access Data From a Cursor |
| 5 | +========================= |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 1 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :keywords: read, results, oplog |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to access data from a **cursor** with |
| 24 | +{+driver-short+}. |
| 25 | + |
| 26 | +A cursor is a mechanism that returns the results of a read operation in iterable |
| 27 | +batches. Because a cursor holds only a subset of documents at any given time, |
| 28 | +cursors reduce both memory consumption and network bandwidth usage. |
| 29 | + |
| 30 | +Whenever {+driver-short+} performs a read operation that returns multiple |
| 31 | +documents, it automatically returns those documents in a cursor. |
| 32 | + |
| 33 | +Sample Data |
| 34 | +~~~~~~~~~~~ |
| 35 | + |
| 36 | +The examples in this guide use the ``sample_restaurants.restaurants`` collection |
| 37 | +from the :atlas:`Atlas sample datasets </sample-data>`. To learn how to create a |
| 38 | +free MongoDB Atlas cluster and load the sample datasets, see the :ref:`<pymongo-get-started>`. |
| 39 | + |
| 40 | +.. _pymongo-cursors-iterate: |
| 41 | + |
| 42 | +Access Cursor Contents Iteratively |
| 43 | +---------------------------------- |
| 44 | + |
| 45 | +To iterate over the contents of a cursor, use a ``for`` loop, as shown in the |
| 46 | +following example: |
| 47 | + |
| 48 | +.. literalinclude:: /includes/cursors/cursors.py |
| 49 | + :start-after: start-cursor-iterate |
| 50 | + :end-before: end-cursor-iterate |
| 51 | + :language: python |
| 52 | + :copyable: |
| 53 | + |
| 54 | +Retrieve Documents Individually |
| 55 | +------------------------------- |
| 56 | + |
| 57 | +Retrieve documents from a cursor individually by calling the ``next()`` method. |
| 58 | + |
| 59 | +The following example finds all documents in a collection with a ``name`` value |
| 60 | +of ``"Dunkin' Donuts"``. It then prints the first document in the cursor by calling the |
| 61 | +``next()`` method. |
| 62 | + |
| 63 | +.. io-code-block:: |
| 64 | + :copyable: |
| 65 | + |
| 66 | + .. input:: /includes/cursors/cursors.py |
| 67 | + :start-after: start-cursor-next |
| 68 | + :end-before: end-cursor-next |
| 69 | + :language: python |
| 70 | + |
| 71 | + .. output:: |
| 72 | + |
| 73 | + {'_id': ObjectId('...'), 'address': { ... }, 'borough': 'Bronx', 'cuisine': 'Donuts', 'grades': [...], 'name': "Dunkin' Donuts", 'restaurant_id': '40379573'} |
| 74 | + |
| 75 | +Retrieve All Documents |
| 76 | +---------------------- |
| 77 | + |
| 78 | +.. warning:: |
| 79 | + |
| 80 | + If the number and size of documents returned by your query exceeds available |
| 81 | + application memory, your program will crash. If you expect a large result |
| 82 | + set, :ref:`access your cursor iteratively <pymongo-cursors-iterate>`. |
| 83 | + |
| 84 | +To retrieve all documents from a cursor, convert the cursor into a ``list`` as |
| 85 | +shown in the following example: |
| 86 | + |
| 87 | +.. literalinclude:: /includes/cursors/cursors.py |
| 88 | + :start-after: start-cursor-list |
| 89 | + :end-before: end-cursor-list |
| 90 | + :language: python |
| 91 | + :copyable: |
| 92 | + :emphasize-lines: 3 |
| 93 | + |
| 94 | +Close a Cursor |
| 95 | +-------------- |
| 96 | + |
| 97 | +By default, MongoDB closes a cursor when the client has exhausted all the |
| 98 | +results in the cursor. To explicitly close a cursor, call the ``close()`` method |
| 99 | +as shown in the following example: |
| 100 | + |
| 101 | +.. literalinclude:: /includes/cursors/cursors.py |
| 102 | + :start-after: start-cursor-close |
| 103 | + :end-before: end-cursor-close |
| 104 | + :emphasize-lines: 5 |
| 105 | + :language: python |
| 106 | + :copyable: |
| 107 | + |
| 108 | +Tailable Cursors |
| 109 | +---------------- |
| 110 | + |
| 111 | +When querying on a :manual:`capped collection </core/capped-collections/>`, you |
| 112 | +can use a **tailable cursor** that remains open after the client exhausts the |
| 113 | +results in a cursor. To create a tailable cursor with capped collection, |
| 114 | +specify ``CursorType.TAILABLE_AWAIT`` in the ``cursor_type`` option of a |
| 115 | +``find()`` method. |
| 116 | + |
| 117 | +The following example uses a tailable cursor to tail the oplog |
| 118 | +of a replica-set member: |
| 119 | + |
| 120 | +.. literalinclude:: /includes/cursors/tailable-cursor.py |
| 121 | + :language: python |
| 122 | + :copyable: |
| 123 | + :emphasize-lines: 8 |
| 124 | + |
| 125 | +To learn more about tailable cursors, see the :manual:`Tailable Cursors guide |
| 126 | +</core/tailable-cursors/>` in the MongoDB Server manual. |
0 commit comments