@@ -4,6 +4,12 @@ Access Data From a Cursor
4
4
5
5
.. default-domain:: mongodb
6
6
7
+ .. contents:: On this page
8
+ :local:
9
+ :backlinks: none
10
+ :depth: 2
11
+ :class: singlecol
12
+
7
13
Read operations that return multiple documents do not immediately return
8
14
all values matching the query. Because a query can potentially match
9
15
large number of documents, we need to be able to access or store the
@@ -25,7 +31,7 @@ instance of a ``FindIterable``.
25
31
26
32
A ``FindIterable`` consists of documents matched by your search criteria
27
33
and allows you to further specify which documents to see by setting
28
- paramaters though methods.
34
+ parameters though methods.
29
35
30
36
Terminal Methods
31
37
----------------
@@ -37,8 +43,8 @@ operation.
37
43
First
38
44
~~~~~
39
45
40
- Use the :java-docs:` first() <apidocs/mongodb-driver-sync/com/mongodb/client/MongoIterable.html# first()>`
41
- method to retireve the first document in your query results:
46
+ Use the `` first()`` method to retrieve the first document in your query
47
+ results:
42
48
43
49
.. literalinclude:: /includes/fundamentals/code-snippets/Cursor.java
44
50
:language: java
@@ -52,8 +58,7 @@ document, such as when filtering by a unique index.
52
58
Into
53
59
~~~~
54
60
55
- Use the :java-docs:`into() <apidocs/mongodb-driver-sync/com/mongodb/client/MongoIterable.html#into(A)>`
56
- method to store your query results in a ``List``:
61
+ Use the ``into()`` method to store your query results in a ``List``:
57
62
58
63
.. literalinclude:: /includes/fundamentals/code-snippets/Cursor.java
59
64
:language: java
@@ -67,9 +72,8 @@ of documents that can fit into available memory.
67
72
Cursor
68
73
~~~~~~
69
74
70
- Use the :java-docs:`cursor() <apidocs/mongodb-driver-sync/com/mongodb/client/MongoIterable.html#cursor()>`
71
- method to iterate through fetched documents and ensure that the cursor
72
- closes if there is an early termination:
75
+ Use the ``cursor()`` method to iterate through fetched documents and
76
+ ensure that the cursor closes if there is an early termination:
73
77
74
78
.. code-block:: java
75
79
:copyable: true
@@ -78,6 +82,51 @@ closes if there is an early termination:
78
82
79
83
For more information on how to ensure a cursor closes, see the :ref:`cursor cleanup section <cursor_cleanup>`.
80
84
85
+ Explain
86
+ ~~~~~~~
87
+
88
+ Use the ``explain()`` method to view information about how MongoDB
89
+ executes your operation.
90
+
91
+ The ``explain()`` method returns **execution plans** and performance
92
+ statistics. An execution plan is a potential way MongoDB
93
+ can complete an operation. The ``explain()`` method provides both the
94
+ winning plan (the plan MongoDB executed) and rejected plans.
95
+
96
+ .. include:: /includes/fundamentals/explain-verbosity.rst
97
+
98
+ The following example prints the JSON representation of the
99
+ winning plan for aggregation stages that produce execution plans:
100
+
101
+ .. literalinclude:: /includes/fundamentals/code-snippets/Cursor.java
102
+ :language: java
103
+ :dedent:
104
+ :start-after: begin explainExample
105
+ :end-before: end explainExample
106
+
107
+ The above code snippet should produce the following output:
108
+
109
+ .. code-block:: none
110
+ :copyable: false
111
+
112
+ { "stage": "COLLSCAN", "direction": "forward" }
113
+
114
+ For more information on the explain operation, see the following
115
+ Server Manual Entries:
116
+
117
+ - :manual:`Explain Output </reference/explain-results/>`
118
+ - :manual:`Query Plans </core/query-plans/>`
119
+
120
+ For more information about the methods and classes mentioned in this
121
+ section, see the following API documentation:
122
+
123
+ - :java-docs:`first() <apidocs/mongodb-driver-sync/com/mongodb/client/MongoIterable.html#first()>`
124
+ - :java-docs:`into() <apidocs/mongodb-driver-sync/com/mongodb/client/MongoIterable.html#into(A)>`
125
+ - :java-docs:`cursor() <apidocs/mongodb-driver-sync/com/mongodb/client/MongoIterable.html#cursor()>`
126
+ - :java-docs:`explain() <apidocs/mongodb-driver-sync/com/mongodb/client/FindIterable.html#explain()>`
127
+ - :java-core-api:`ExplainVerbosity <com/mongodb/ExplainVerbosity>`
128
+
129
+
81
130
Usage Patterns
82
131
--------------
83
132
0 commit comments