@@ -99,27 +99,50 @@ public void testExplainOfAggregateWithNewResponseStructure() {
99
99
AggregateIterable <BsonDocument > iterable = collection
100
100
.aggregate (singletonList (Aggregates .match (Filters .eq ("_id" , 1 ))));
101
101
102
- Document explainDocument = iterable .explain ();
103
- assertNotNull (explainDocument );
102
+ Document explainDocument = getAggregateExplainDocument (iterable .explain ());
104
103
assertTrue (explainDocument .containsKey ("queryPlanner" ));
105
104
assertTrue (explainDocument .containsKey ("executionStats" ));
106
105
107
- explainDocument = iterable .explain (ExplainVerbosity .QUERY_PLANNER );
106
+ explainDocument = getAggregateExplainDocument ( iterable .explain (ExplainVerbosity .QUERY_PLANNER ) );
108
107
assertNotNull (explainDocument );
109
108
assertTrue (explainDocument .containsKey ("queryPlanner" ));
110
109
assertFalse (explainDocument .containsKey ("executionStats" ));
111
110
112
- BsonDocument explainBsonDocument = iterable .explain (BsonDocument .class );
111
+ BsonDocument explainBsonDocument = getAggregateExplainDocument ( iterable .explain (BsonDocument .class ) );
113
112
assertNotNull (explainBsonDocument );
114
113
assertTrue (explainBsonDocument .containsKey ("queryPlanner" ));
115
114
assertTrue (explainBsonDocument .containsKey ("executionStats" ));
116
115
117
- explainBsonDocument = iterable .explain (BsonDocument .class , ExplainVerbosity .QUERY_PLANNER );
116
+ explainBsonDocument = getAggregateExplainDocument ( iterable .explain (BsonDocument .class , ExplainVerbosity .QUERY_PLANNER ) );
118
117
assertNotNull (explainBsonDocument );
119
118
assertTrue (explainBsonDocument .containsKey ("queryPlanner" ));
120
119
assertFalse (explainBsonDocument .containsKey ("executionStats" ));
121
120
}
122
121
122
+ // Post-MongoDB 7.0, sharded cluster responses move the explain plan document into a "shards" document, which a plan for each shard.
123
+ // This method grabs the explain plan document from the first shard when this new structure is present.
124
+ private static Document getAggregateExplainDocument (final Document rootAggregateExplainDocument ) {
125
+ assertNotNull (rootAggregateExplainDocument );
126
+ Document aggregateExplainDocument = rootAggregateExplainDocument ;
127
+ if (rootAggregateExplainDocument .containsKey ("shards" )) {
128
+ Document shardDocument = rootAggregateExplainDocument .get ("shards" , Document .class );
129
+ String firstKey = shardDocument .keySet ().iterator ().next ();
130
+ aggregateExplainDocument = shardDocument .get (firstKey , Document .class );
131
+ }
132
+ return aggregateExplainDocument ;
133
+ }
134
+
135
+ private static BsonDocument getAggregateExplainDocument (final BsonDocument rootAggregateExplainDocument ) {
136
+ assertNotNull (rootAggregateExplainDocument );
137
+ BsonDocument aggregateExplainDocument = rootAggregateExplainDocument ;
138
+ if (rootAggregateExplainDocument .containsKey ("shards" )) {
139
+ BsonDocument shardDocument = rootAggregateExplainDocument .getDocument ("shards" );
140
+ String firstKey = shardDocument .getFirstKey ();
141
+ aggregateExplainDocument = shardDocument .getDocument (firstKey );
142
+ }
143
+ return aggregateExplainDocument ;
144
+ }
145
+
123
146
@ Test
124
147
public void testExplainOfAggregateWithOldResponseStructure () {
125
148
// Aggregate explain is supported on earlier versions, but the structure of the response on which we're asserting in this test
0 commit comments