|
| 1 | +.. _java-rs-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** by using the |
| 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 | +In the {+driver-short+}, some streams are backed by cursors. The size of batches used |
| 31 | +in these underlying cursors depends on the demand requested on the ``Subscription`` for the |
| 32 | +``Publisher``. The batch size of data contained by each underlying cursor can be set by |
| 33 | +using the ``FindPublisher.batchSize()`` method. |
| 34 | + |
| 35 | +Sample Data |
| 36 | +~~~~~~~~~~~ |
| 37 | + |
| 38 | +The examples in this guide use the ``sample_restaurants.restaurants`` collection |
| 39 | +from the :atlas:`Atlas sample datasets </sample-data>`. To learn how to create a |
| 40 | +free MongoDB Atlas cluster and load the sample datasets, see the |
| 41 | +:ref:`<java-rs-getting-started>` guide. |
| 42 | + |
| 43 | +.. include:: includes/reactor-note.rst |
| 44 | + |
| 45 | +.. _java-rs-cursors-iterate: |
| 46 | + |
| 47 | +Access Cursor Contents Iteratively |
| 48 | +---------------------------------- |
| 49 | + |
| 50 | +To iterate over the contents of a cursor, use the ``Flux.from()`` method, as shown in the |
| 51 | +following example: |
| 52 | + |
| 53 | +.. literalinclude:: /includes/read-ops/cursors.java |
| 54 | + :start-after: start-cursor-iterate |
| 55 | + :end-before: end-cursor-iterate |
| 56 | + :language: java |
| 57 | + :dedent: |
| 58 | + :copyable: |
| 59 | + |
| 60 | +Retrieve All Documents |
| 61 | +---------------------- |
| 62 | + |
| 63 | +.. warning:: |
| 64 | + |
| 65 | + If the number and size of documents returned by your query exceeds available |
| 66 | + application memory, your program will crash. If you expect a large result |
| 67 | + set, :ref:`access your cursor iteratively <java-rs-cursors-iterate>`. |
| 68 | + |
| 69 | +To retrieve all documents from a cursor, convert the cursor into a ``List``, as |
| 70 | +shown in the following example: |
| 71 | + |
| 72 | +.. literalinclude:: /includes/read-ops/cursors.java |
| 73 | + :start-after: start-cursor-list |
| 74 | + :end-before: end-cursor-list |
| 75 | + :language: java |
| 76 | + :dedent: |
| 77 | + :copyable: |
| 78 | + |
| 79 | +Tailable Cursors |
| 80 | +---------------- |
| 81 | + |
| 82 | +When querying on a :manual:`capped collection </core/capped-collections/>`, you |
| 83 | +can use a **tailable cursor** that remains open after the client exhausts the |
| 84 | +results in a cursor. To create a tailable cursor on a capped collection, |
| 85 | +pass a value of ``CursorType.TailableAwait`` to the ``cursorType()`` method of a |
| 86 | +``FindPublisher`` object. |
| 87 | + |
| 88 | +The following example creates a tailable cursor on a collection and prints its contents: |
| 89 | + |
| 90 | +.. literalinclude:: /includes/read-ops/cursors.java |
| 91 | + :start-after: start-tailable-cursor |
| 92 | + :end-before: end-tailable-cursor |
| 93 | + :language: java |
| 94 | + :dedent: |
| 95 | + :copyable: |
| 96 | + |
| 97 | +To learn more about tailable cursors and their usage, see the :manual:`Tailable Cursors guide |
| 98 | +</core/tailable-cursors/>` in the {+mdb-server+} manual. |
| 99 | + |
| 100 | +API Documentation |
| 101 | +----------------- |
| 102 | + |
| 103 | +To learn more about any of the methods or types discussed in this |
| 104 | +guide, see the following API documentation: |
| 105 | + |
| 106 | +- `find() <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoCollection.html#find()>`__ |
| 107 | +- `FindPublisher <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/FindPublisher.html>`__ |
| 108 | +- `CursorType <{+api+}/mongodb-driver-core/com/mongodb/CursorType.html>`__ |
0 commit comments