Skip to content

Commit b836511

Browse files
DOCSP-13820 explain method (#92)
* added explain method
1 parent 6e5d8ae commit b836511

File tree

5 files changed

+156
-9
lines changed

5 files changed

+156
-9
lines changed

source/fundamentals/aggregation.txt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,49 @@ The above aggregation should produce the following results:
150150
{"_id": 4, "count": 2}
151151
{"_id": 5, "count": 1}
152152

153+
Explain Aggregation Example
154+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
155+
156+
To view information about how MongoDB executes your operation, use the
157+
``explain()`` method of the ``AggregateIterable`` class. The ``explain()``
158+
method returns **execution plans** and performance statistics. An execution
159+
plan is a potential way MongoDB can complete an operation.
160+
The ``explain()`` method provides both the winning plan (the plan MongoDB
161+
executed) and rejected plans.
162+
163+
.. include:: /includes/fundamentals/explain-verbosity.rst
164+
165+
In the following example, we print the JSON representation of the
166+
winning plan for aggregation stages that produce execution plans:
167+
168+
.. literalinclude:: /includes/fundamentals/code-snippets/AggTour.java
169+
:language: java
170+
:dedent:
171+
:start-after: begin aggregation three
172+
:end-before: end aggregation three
173+
174+
The above code snippet should produce the following output:
175+
176+
.. code-block:: none
177+
:copyable: false
178+
179+
{ "stage": "PROJECTION_SIMPLE",
180+
"transformBy": {"stars": 1, "_id": 0},
181+
"inputStage": {
182+
"stage": "COLLSCAN",
183+
"filter": {
184+
"categories": {"$eq":"bakery"}},
185+
"direction": "forward"}}
186+
187+
For more information about the topics mentioned in this section, see the
188+
following resources:
189+
190+
- :manual:`Explain Output </reference/explain-results/>` Server Manual Entry
191+
- :manual:`Query Plans </core/query-plans/>` Server Manual Entry
192+
- :java-core-api:`ExplainVerbosity <com/mongodb/ExplainVerbosity>` API Documentation
193+
- :java-docs:`explain() <apidocs/mongodb-driver-sync/com/mongodb/client/FindIterable.html#explain()>` API Documentation
194+
- :java-docs:`AggregateIterable <apidocs/mongodb-driver-sync/com/mongodb/client/AggregateIterable.html>` API Documentation
195+
153196
Aggregation Expression Example
154197
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
155198

source/fundamentals/crud/read-operations/cursor.txt

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ Access Data From a Cursor
44

55
.. default-domain:: mongodb
66

7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
713
Read operations that return multiple documents do not immediately return
814
all values matching the query. Because a query can potentially match
915
large number of documents, we need to be able to access or store the
@@ -25,7 +31,7 @@ instance of a ``FindIterable``.
2531

2632
A ``FindIterable`` consists of documents matched by your search criteria
2733
and allows you to further specify which documents to see by setting
28-
paramaters though methods.
34+
parameters though methods.
2935

3036
Terminal Methods
3137
----------------
@@ -37,8 +43,8 @@ operation.
3743
First
3844
~~~~~
3945

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:
4248

4349
.. literalinclude:: /includes/fundamentals/code-snippets/Cursor.java
4450
:language: java
@@ -52,8 +58,7 @@ document, such as when filtering by a unique index.
5258
Into
5359
~~~~
5460

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``:
5762

5863
.. literalinclude:: /includes/fundamentals/code-snippets/Cursor.java
5964
:language: java
@@ -67,9 +72,8 @@ of documents that can fit into available memory.
6772
Cursor
6873
~~~~~~
6974

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:
7377

7478
.. code-block:: java
7579
:copyable: true
@@ -78,6 +82,51 @@ closes if there is an early termination:
7882

7983
For more information on how to ensure a cursor closes, see the :ref:`cursor cleanup section <cursor_cleanup>`.
8084

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+
81130
Usage Patterns
82131
--------------
83132

source/includes/fundamentals/code-snippets/AggTour.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.mongodb.client.MongoClients;
77
import com.mongodb.client.MongoCollection;
88
import com.mongodb.client.MongoDatabase;
9+
import com.mongodb.ExplainVerbosity;
910
import com.mongodb.client.model.Accumulators;
1011
import com.mongodb.client.model.Aggregates;
1112
import com.mongodb.client.model.Filters;
@@ -22,7 +23,7 @@ public class AggTour {
2223
public static void main(String[] args) {
2324
// Replace the uri string with your MongoDB deployment's connection string
2425
final String uri = "mongodb+srv://<user>:<password>@<cluster-url>?retryWrites=true&w=majority";
25-
26+
2627
MongoClient mongoClient = MongoClients.create(uri);
2728
MongoDatabase database = mongoClient.getDatabase("aggregation");
2829
MongoCollection<Document> collection = database.getCollection("restaurants");
@@ -52,6 +53,24 @@ public static void main(String[] args) {
5253
)
5354
).forEach(doc -> System.out.println(doc.toJson()));
5455
// end aggregation one
56+
// begin aggregation three
57+
Document explanation = collection.aggregate(
58+
Arrays.asList(
59+
Aggregates.match(Filters.eq("categories", "bakery")),
60+
Aggregates.group("$stars", Accumulators.sum("count", 1))
61+
)
62+
).explain(ExplainVerbosity.EXECUTION_STATS);
63+
64+
List<Document> stages = explanation.get("stages", List.class);
65+
List<String> keys = Arrays.asList("queryPlanner", "winningPlan");
66+
67+
for (Document stage : stages) {
68+
Document cursorStage = stage.get("$cursor", Document.class);
69+
if (cursorStage != null) {
70+
System.out.println(cursorStage.getEmbedded(keys, Document.class).toJson());
71+
}
72+
}
73+
// end aggregation three
5574
// begin aggregation two
5675
collection.aggregate(
5776
Arrays.asList(

source/includes/fundamentals/code-snippets/Cursor.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.mongodb.client.MongoClients;
55
import com.mongodb.client.MongoCollection;
66
import com.mongodb.client.MongoDatabase;
7+
import com.mongodb.ExplainVerbosity;
78
import com.mongodb.client.*;
89

910
import org.bson.conversions.Bson;
@@ -38,6 +39,9 @@ public static void main(String [] args){
3839
System.out.println("First Example");
3940
c.firstExample();
4041

42+
System.out.println("Explain Example");
43+
c.explainExample();
44+
4145
System.out.println("Into Example");
4246
c.intoExample();
4347

@@ -65,6 +69,14 @@ private void firstExample(){
6569
// end firstExample
6670
}
6771

72+
private void explainExample(){
73+
// begin explainExample
74+
Document explanation = collection.find().explain(ExplainVerbosity.EXECUTION_STATS);
75+
List<String> keys = Arrays.asList("queryPlanner", "winningPlan");
76+
System.out.println(explanation.getEmbedded(keys, Document.class).toJson());
77+
// end explainExample
78+
}
79+
6880
private void intoExample(){
6981
// begin intoExample
7082
List<Document> results = new ArrayList<>();
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
You can specify the level of detail of your explanation by passing a
2+
verbosity level to the ``explain()`` method.
3+
4+
The following table shows all verbosity levels for explanations and
5+
their intended use cases:
6+
7+
.. list-table::
8+
:header-rows: 1
9+
:stub-columns: 1
10+
:widths: 40 60
11+
12+
* - Verbosity Level
13+
- Use Case
14+
15+
* - ALL_PLANS_EXECUTIONS
16+
- You want to know which plan MongoDB will choose to run your query.
17+
18+
* - EXECUTION_STATS
19+
- You want to know if your query is performing well.
20+
21+
* - QUERY_PLANNER
22+
- You have a problem with your query and you want as much information
23+
as possible to diagnose the issue.
24+

0 commit comments

Comments
 (0)