1717package com .mongodb .client .model .changestream ;
1818
1919import com .mongodb .MongoNamespace ;
20- import com .mongodb .assertions .Assertions ;
2120import com .mongodb .lang .Nullable ;
2221import org .bson .BsonDocument ;
2322import org .bson .BsonInt64 ;
24- import org .bson .BsonString ;
2523import org .bson .BsonTimestamp ;
2624import org .bson .codecs .Codec ;
2725import org .bson .codecs .configuration .CodecRegistry ;
3028import org .bson .codecs .pojo .annotations .BsonIgnore ;
3129import org .bson .codecs .pojo .annotations .BsonProperty ;
3230
31+ import java .util .Objects ;
32+
3333/**
3434 * Represents the {@code $changeStream} aggregation output document.
3535 *
@@ -48,6 +48,9 @@ public final class ChangeStreamDocument<TDocument> {
4848 private final TDocument fullDocument ;
4949 private final BsonDocument documentKey ;
5050 private final BsonTimestamp clusterTime ;
51+ @ BsonProperty ("operationType" )
52+ private final String operationTypeString ;
53+ @ BsonIgnore
5154 private final OperationType operationType ;
5255 private final UpdateDescription updateDescription ;
5356 private final BsonInt64 txnNumber ;
@@ -56,7 +59,7 @@ public final class ChangeStreamDocument<TDocument> {
5659 /**
5760 * Creates a new instance
5861 *
59- * @param operationType the operation type
62+ * @param operationTypeString the operation type
6063 * @param resumeToken the resume token
6164 * @param namespaceDocument the BsonDocument representing the namespace
6265 * @param destinationNamespaceDocument the BsonDocument representing the destinatation namespace
@@ -67,10 +70,10 @@ public final class ChangeStreamDocument<TDocument> {
6770 * @param txnNumber the transaction number
6871 * @param lsid the identifier for the session associated with the transaction
6972 *
70- * @since 3.11
73+ * @since 4.6
7174 */
7275 @ BsonCreator
73- public ChangeStreamDocument (@ BsonProperty ("operationType" ) final OperationType operationType ,
76+ public ChangeStreamDocument (@ BsonProperty ("operationType" ) final String operationTypeString ,
7477 @ BsonProperty ("resumeToken" ) final BsonDocument resumeToken ,
7578 @ Nullable @ BsonProperty ("ns" ) final BsonDocument namespaceDocument ,
7679 @ Nullable @ BsonProperty ("to" ) final BsonDocument destinationNamespaceDocument ,
@@ -86,16 +89,42 @@ public ChangeStreamDocument(@BsonProperty("operationType") final OperationType o
8689 this .documentKey = documentKey ;
8790 this .fullDocument = fullDocument ;
8891 this .clusterTime = clusterTime ;
89- this .operationType = operationType ;
92+ this .operationTypeString = operationTypeString ;
93+ this .operationType = OperationType .fromString (operationTypeString );
9094 this .updateDescription = updateDescription ;
9195 this .txnNumber = txnNumber ;
9296 this .lsid = lsid ;
9397 }
9498
95- private static BsonDocument namespaceToDocument (final MongoNamespace namespace ) {
96- Assertions .notNull ("namespace" , namespace );
97- return new BsonDocument ("db" , new BsonString (namespace .getDatabaseName ()))
98- .append ("coll" , new BsonString (namespace .getCollectionName ()));
99+ /**
100+ * Creates a new instance
101+ *
102+ * @param operationType the operation type
103+ * @param resumeToken the resume token
104+ * @param namespaceDocument the BsonDocument representing the namespace
105+ * @param destinationNamespaceDocument the BsonDocument representing the destinatation namespace
106+ * @param fullDocument the full document
107+ * @param documentKey a document containing the _id of the changed document
108+ * @param clusterTime the cluster time at which the change occured
109+ * @param updateDescription the update description
110+ * @param txnNumber the transaction number
111+ * @param lsid the identifier for the session associated with the transaction
112+ *
113+ * @since 3.11
114+ */
115+ @ Deprecated
116+ public ChangeStreamDocument (final OperationType operationType ,
117+ final BsonDocument resumeToken ,
118+ final BsonDocument namespaceDocument ,
119+ final BsonDocument destinationNamespaceDocument ,
120+ final TDocument fullDocument ,
121+ final BsonDocument documentKey ,
122+ final BsonTimestamp clusterTime ,
123+ final UpdateDescription updateDescription ,
124+ final BsonInt64 txnNumber ,
125+ final BsonDocument lsid ) {
126+ this (operationType .getValue (), resumeToken , namespaceDocument , destinationNamespaceDocument , fullDocument , documentKey ,
127+ clusterTime , updateDescription , txnNumber , lsid );
99128 }
100129
101130 /**
@@ -240,6 +269,24 @@ public BsonTimestamp getClusterTime() {
240269 return clusterTime ;
241270 }
242271
272+
273+ /**
274+ * Returns the operation type as a string.
275+ *
276+ * <p>
277+ * This method is useful when using a driver release that has not yet been updated to include a newer operation type in the
278+ * {@link OperationType} enum. In that case, {@link #getOperationType()} will return {@link OperationType#OTHER} and this method can
279+ * be used to retrieve the actual operation type as a string value.
280+ * </p>
281+ *
282+ * @return the operation type as a string
283+ * @since 4.6
284+ * @see #getOperationType()
285+ */
286+ public String getOperationTypeString () {
287+ return operationTypeString ;
288+ }
289+
243290 /**
244291 * Returns the operationType
245292 *
@@ -324,10 +371,10 @@ public boolean equals(final Object o) {
324371 if (documentKey != null ? !documentKey .equals (that .documentKey ) : that .documentKey != null ) {
325372 return false ;
326373 }
327- if (clusterTime != null ? ! clusterTime .equals (that . clusterTime ) : that .clusterTime != null ) {
374+ if (! Objects .equals (operationTypeString , that .operationTypeString ) ) {
328375 return false ;
329376 }
330- if (operationType != that .operationType ) {
377+ if (clusterTime != null ? ! clusterTime . equals ( that .clusterTime ) : that . clusterTime != null ) {
331378 return false ;
332379 }
333380 if (updateDescription != null ? !updateDescription .equals (that .updateDescription ) : that .updateDescription != null ) {
@@ -351,7 +398,7 @@ public int hashCode() {
351398 result = 31 * result + (fullDocument != null ? fullDocument .hashCode () : 0 );
352399 result = 31 * result + (documentKey != null ? documentKey .hashCode () : 0 );
353400 result = 31 * result + (clusterTime != null ? clusterTime .hashCode () : 0 );
354- result = 31 * result + (operationType != null ? operationType .hashCode () : 0 );
401+ result = 31 * result + (operationTypeString != null ? operationTypeString .hashCode () : 0 );
355402 result = 31 * result + (updateDescription != null ? updateDescription .hashCode () : 0 );
356403 result = 31 * result + (txnNumber != null ? txnNumber .hashCode () : 0 );
357404 result = 31 * result + (lsid != null ? lsid .hashCode () : 0 );
@@ -361,7 +408,7 @@ public int hashCode() {
361408 @ Override
362409 public String toString () {
363410 return "ChangeStreamDocument{"
364- + " operationType=" + operationType
411+ + " operationType=" + operationTypeString
365412 + ", resumeToken=" + resumeToken
366413 + ", namespace=" + getNamespace ()
367414 + ", destinationNamespace=" + getDestinationNamespace ()
0 commit comments