Skip to content

Commit 6ce34dc

Browse files
authored
DOCSP-39705: Cursors (#72)
1 parent cba57be commit 6ce34dc

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
lines changed

source/includes/read-ops/cursors.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import com.mongodb.ConnectionString;
2+
import com.mongodb.CursorType;
3+
import com.mongodb.MongoClientSettings;
4+
import com.mongodb.client.model.Filters;
5+
import com.mongodb.reactivestreams.client.*;
6+
import org.bson.Document;
7+
import reactor.core.publisher.Flux;
8+
9+
import java.util.List;
10+
11+
public class Cursors {
12+
public static void main(String[] args) {
13+
String uri = "<connection string URI>";
14+
15+
MongoClientSettings settings = MongoClientSettings.builder()
16+
.applyConnectionString(new ConnectionString(uri))
17+
.build();
18+
19+
try (MongoClient mongoClient = MongoClients.create(settings))
20+
{
21+
MongoDatabase database = mongoClient.getDatabase("sample_restaurants");
22+
MongoCollection<Document> collection = database.getCollection("restaurants");
23+
24+
// start-cursor-iterate
25+
FindPublisher<Document> findPublisher = collection.find();
26+
Flux.from(findPublisher)
27+
.doOnNext(x -> System.out.println(x.getString("name")))
28+
.blockLast();
29+
// end-cursor-iterate
30+
31+
// start-cursor-list
32+
FindPublisher<Document> findPublisher = collection.find(Filters.eq("name", "Dunkin' Donuts"));
33+
List<Document> resultsList = Flux.from(findPublisher).collectList().block();
34+
// end-cursor-list
35+
36+
// start-tailable-cursor
37+
FindPublisher<Document> findPublisher = collection.find().cursorType(CursorType.TailableAwait);
38+
Flux.from(findPublisher)
39+
.doOnNext(System.out::println)
40+
.blockLast();
41+
// end-tailable-cursor
42+
}
43+
}

source/read-data-from-mongo.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Read Data From MongoDB
2828
/read/specify-documents-to-return
2929
/read/count-documents
3030
/read/distinct
31+
/read/cursors
3132
/read/read-preference
3233
/read/text-search
3334
/read/geo

source/read/cursors.txt

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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

Comments
 (0)