@@ -77,13 +77,13 @@ public final class CommandMessage extends RequestMessage {
7777 */
7878 private static final byte PAYLOAD_TYPE_1_DOCUMENT_SEQUENCE = 1 ;
7979
80- private final MongoNamespace namespace ;
8180 private final BsonDocument command ;
8281 private final FieldNameValidator commandFieldNameValidator ;
8382 private final ReadPreference readPreference ;
8483 private final boolean exhaustAllowed ;
8584 private final MessageSequences sequences ;
8685 private final boolean responseExpected ;
86+ private final String database ;
8787 /**
8888 * {@code null} iff either {@link #sequences} is not of the {@link DualMessageSequences} type,
8989 * or it is of that type, but it has not been {@linkplain #encodeMessageBodyWithMetadata(ByteBufferBsonOutput, OperationContext) encoded}.
@@ -93,35 +93,35 @@ public final class CommandMessage extends RequestMessage {
9393 private final ClusterConnectionMode clusterConnectionMode ;
9494 private final ServerApi serverApi ;
9595
96- CommandMessage (final MongoNamespace namespace , final BsonDocument command , final FieldNameValidator commandFieldNameValidator ,
96+ CommandMessage (final String database , final BsonDocument command , final FieldNameValidator commandFieldNameValidator ,
9797 final ReadPreference readPreference , final MessageSettings settings , final ClusterConnectionMode clusterConnectionMode ,
9898 @ Nullable final ServerApi serverApi ) {
99- this (namespace , command , commandFieldNameValidator , readPreference , settings , true , EmptyMessageSequences .INSTANCE ,
99+ this (database , command , commandFieldNameValidator , readPreference , settings , true , EmptyMessageSequences .INSTANCE ,
100100 clusterConnectionMode , serverApi );
101101 }
102102
103- CommandMessage (final MongoNamespace namespace , final BsonDocument command , final FieldNameValidator commandFieldNameValidator ,
103+ CommandMessage (final String database , final BsonDocument command , final FieldNameValidator commandFieldNameValidator ,
104104 final ReadPreference readPreference , final MessageSettings settings , final boolean exhaustAllowed ,
105105 final ClusterConnectionMode clusterConnectionMode , @ Nullable final ServerApi serverApi ) {
106- this (namespace , command , commandFieldNameValidator , readPreference , settings , true , exhaustAllowed , EmptyMessageSequences .INSTANCE ,
106+ this (database , command , commandFieldNameValidator , readPreference , settings , true , exhaustAllowed , EmptyMessageSequences .INSTANCE ,
107107 clusterConnectionMode , serverApi );
108108 }
109109
110- CommandMessage (final MongoNamespace namespace , final BsonDocument command , final FieldNameValidator commandFieldNameValidator ,
110+ CommandMessage (final String database , final BsonDocument command , final FieldNameValidator commandFieldNameValidator ,
111111 final ReadPreference readPreference , final MessageSettings settings , final boolean responseExpected ,
112112 final MessageSequences sequences ,
113113 final ClusterConnectionMode clusterConnectionMode , @ Nullable final ServerApi serverApi ) {
114- this (namespace , command , commandFieldNameValidator , readPreference , settings , responseExpected , false ,
114+ this (database , command , commandFieldNameValidator , readPreference , settings , responseExpected , false ,
115115 sequences , clusterConnectionMode , serverApi );
116116 }
117117
118- CommandMessage (final MongoNamespace namespace , final BsonDocument command , final FieldNameValidator commandFieldNameValidator ,
118+ CommandMessage (final String database , final BsonDocument command , final FieldNameValidator commandFieldNameValidator ,
119119 final ReadPreference readPreference , final MessageSettings settings ,
120120 final boolean responseExpected , final boolean exhaustAllowed ,
121121 final MessageSequences sequences ,
122122 final ClusterConnectionMode clusterConnectionMode , @ Nullable final ServerApi serverApi ) {
123- super (namespace . getFullName (), getOpCode (settings , clusterConnectionMode , serverApi ), settings );
124- this .namespace = namespace ;
123+ super (getOpCode (settings , clusterConnectionMode , serverApi ), settings );
124+ this .database = database ;
125125 this .command = command ;
126126 this .commandFieldNameValidator = commandFieldNameValidator ;
127127 this .readPreference = readPreference ;
@@ -222,10 +222,6 @@ boolean isResponseExpected() {
222222 }
223223 }
224224
225- MongoNamespace getNamespace () {
226- return namespace ;
227- }
228-
229225 @ Override
230226 protected EncodingMetadata encodeMessageBodyWithMetadata (final ByteBufferBsonOutput bsonOutput , final OperationContext operationContext ) {
231227 int commandStartPosition = useOpMsg () ? writeOpMsg (bsonOutput , operationContext ) : writeOpQuery (bsonOutput );
@@ -281,7 +277,7 @@ private int writeOpMsg(final ByteBufferBsonOutput bsonOutput, final OperationCon
281277
282278 private int writeOpQuery (final ByteBufferBsonOutput bsonOutput ) {
283279 bsonOutput .writeInt32 (0 );
284- bsonOutput .writeCString (namespace .getFullName ());
280+ bsonOutput .writeCString (new MongoNamespace ( getDatabase (), "$cmd" ) .getFullName ());
285281 bsonOutput .writeInt32 (0 );
286282 bsonOutput .writeInt32 (-1 );
287283
@@ -328,7 +324,7 @@ private List<BsonElement> getExtraElements(final OperationContext operationConte
328324 extraElements .add (new BsonElement ("maxTimeMS" , new BsonInt64 (maxTimeMS )))
329325 );
330326 }
331- extraElements .add (new BsonElement ("$db" , new BsonString (new MongoNamespace ( getCollectionName ()). getDatabaseName ())));
327+ extraElements .add (new BsonElement ("$db" , new BsonString (getDatabase ())));
332328 if (sessionContext .getClusterTime () != null ) {
333329 extraElements .add (new BsonElement ("$clusterTime" , sessionContext .getClusterTime ()));
334330 }
@@ -411,6 +407,15 @@ private static boolean isServerVersionKnown(final MessageSettings settings) {
411407 return settings .getMaxWireVersion () != UNKNOWN_WIRE_VERSION ;
412408 }
413409
410+ /**
411+ * Gets the database name
412+ *
413+ * @return the database name
414+ */
415+ public String getDatabase () {
416+ return database ;
417+ }
418+
414419 @ FunctionalInterface
415420 private interface FinishOpMsgSectionWithPayloadType1 extends AutoCloseable {
416421 void close ();
0 commit comments