@@ -99,27 +99,50 @@ public void testExplainOfAggregateWithNewResponseStructure() {
9999 AggregateIterable <BsonDocument > iterable = collection
100100 .aggregate (singletonList (Aggregates .match (Filters .eq ("_id" , 1 ))));
101101
102- Document explainDocument = iterable .explain ();
103- assertNotNull (explainDocument );
102+ Document explainDocument = getAggregateExplainDocument (iterable .explain ());
104103 assertTrue (explainDocument .containsKey ("queryPlanner" ));
105104 assertTrue (explainDocument .containsKey ("executionStats" ));
106105
107- explainDocument = iterable .explain (ExplainVerbosity .QUERY_PLANNER );
106+ explainDocument = getAggregateExplainDocument ( iterable .explain (ExplainVerbosity .QUERY_PLANNER ) );
108107 assertNotNull (explainDocument );
109108 assertTrue (explainDocument .containsKey ("queryPlanner" ));
110109 assertFalse (explainDocument .containsKey ("executionStats" ));
111110
112- BsonDocument explainBsonDocument = iterable .explain (BsonDocument .class );
111+ BsonDocument explainBsonDocument = getAggregateExplainDocument ( iterable .explain (BsonDocument .class ) );
113112 assertNotNull (explainBsonDocument );
114113 assertTrue (explainBsonDocument .containsKey ("queryPlanner" ));
115114 assertTrue (explainBsonDocument .containsKey ("executionStats" ));
116115
117- explainBsonDocument = iterable .explain (BsonDocument .class , ExplainVerbosity .QUERY_PLANNER );
116+ explainBsonDocument = getAggregateExplainDocument ( iterable .explain (BsonDocument .class , ExplainVerbosity .QUERY_PLANNER ) );
118117 assertNotNull (explainBsonDocument );
119118 assertTrue (explainBsonDocument .containsKey ("queryPlanner" ));
120119 assertFalse (explainBsonDocument .containsKey ("executionStats" ));
121120 }
122121
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+
123146 @ Test
124147 public void testExplainOfAggregateWithOldResponseStructure () {
125148 // Aggregate explain is supported on earlier versions, but the structure of the response on which we're asserting in this test
0 commit comments