1717package com .mongodb .client .model .changestream ;
1818
1919import com .mongodb .MongoNamespace ;
20+ import com .mongodb .assertions .Assertions ;
2021import com .mongodb .lang .Nullable ;
2122import org .bson .BsonDocument ;
23+ import org .bson .BsonString ;
2224import org .bson .BsonTimestamp ;
2325import org .bson .codecs .Codec ;
2426import org .bson .codecs .configuration .CodecRegistry ;
2527import org .bson .codecs .pojo .annotations .BsonCreator ;
2628import org .bson .codecs .pojo .annotations .BsonId ;
29+ import org .bson .codecs .pojo .annotations .BsonIgnore ;
2730import org .bson .codecs .pojo .annotations .BsonProperty ;
2831
2932/**
@@ -39,8 +42,7 @@ public final class ChangeStreamDocument<TDocument> {
3942
4043 @ BsonId ()
4144 private final BsonDocument resumeToken ;
42- @ BsonProperty ("ns" )
43- private final MongoNamespace namespace ;
45+ private final BsonDocument namespaceDocument ;
4446 private final TDocument fullDocument ;
4547 private final BsonDocument documentKey ;
4648 private final BsonTimestamp clusterTime ;
@@ -60,12 +62,12 @@ public final class ChangeStreamDocument<TDocument> {
6062 * UpdateDescription)}
6163 */
6264 @ Deprecated
63- public ChangeStreamDocument (@ BsonProperty ( "resumeToken" ) final BsonDocument resumeToken ,
64- @ BsonProperty ( "namespace" ) final MongoNamespace namespace ,
65- @ BsonProperty ( "fullDocument" ) final TDocument fullDocument ,
66- @ BsonProperty ( "documentKey" ) final BsonDocument documentKey ,
67- @ BsonProperty ( "operationType" ) final OperationType operationType ,
68- @ BsonProperty ( "updateDescription" ) final UpdateDescription updateDescription ) {
65+ public ChangeStreamDocument (final BsonDocument resumeToken ,
66+ final MongoNamespace namespace ,
67+ final TDocument fullDocument ,
68+ final BsonDocument documentKey ,
69+ final OperationType operationType ,
70+ final UpdateDescription updateDescription ) {
6971 this (resumeToken , namespace , fullDocument , documentKey , null , operationType , updateDescription );
7072 }
7173
@@ -80,23 +82,54 @@ public ChangeStreamDocument(@BsonProperty("resumeToken") final BsonDocument resu
8082 * @param operationType the operation type
8183 * @param updateDescription the update description
8284 */
85+ @ Deprecated
86+ public ChangeStreamDocument (final BsonDocument resumeToken ,
87+ final MongoNamespace namespace ,
88+ final TDocument fullDocument ,
89+ final BsonDocument documentKey ,
90+ @ Nullable final BsonTimestamp clusterTime ,
91+ final OperationType operationType ,
92+ final UpdateDescription updateDescription ) {
93+ this (resumeToken , namespaceToDocument (namespace ), fullDocument , documentKey ,
94+ clusterTime , operationType , updateDescription );
95+ }
96+
97+ /**
98+ * Creates a new instance
99+ *
100+ * @param resumeToken the resume token
101+ * @param namespaceDocument the BsonDocument representing the namespace
102+ * @param fullDocument the full document
103+ * @param documentKey a document containing the _id of the changed document
104+ * @param clusterTime the cluster time at which the change occured
105+ * @param operationType the operation type
106+ * @param updateDescription the update description
107+ *
108+ * @since 3.8
109+ */
83110 @ BsonCreator
84111 public ChangeStreamDocument (@ BsonProperty ("resumeToken" ) final BsonDocument resumeToken ,
85- @ BsonProperty ("namespace " ) final MongoNamespace namespace ,
112+ @ BsonProperty ("ns " ) final BsonDocument namespaceDocument ,
86113 @ BsonProperty ("fullDocument" ) final TDocument fullDocument ,
87114 @ BsonProperty ("documentKey" ) final BsonDocument documentKey ,
88115 @ Nullable @ BsonProperty ("clusterTime" ) final BsonTimestamp clusterTime ,
89116 @ BsonProperty ("operationType" ) final OperationType operationType ,
90117 @ BsonProperty ("updateDescription" ) final UpdateDescription updateDescription ) {
91118 this .resumeToken = resumeToken ;
92- this .namespace = namespace ;
119+ this .namespaceDocument = namespaceDocument ;
93120 this .documentKey = documentKey ;
94121 this .fullDocument = fullDocument ;
95122 this .clusterTime = clusterTime ;
96123 this .operationType = operationType ;
97124 this .updateDescription = updateDescription ;
98125 }
99126
127+ private static BsonDocument namespaceToDocument (final MongoNamespace namespace ) {
128+ Assertions .notNull ("namespace" , namespace );
129+ return new BsonDocument ("db" , new BsonString (namespace .getDatabaseName ()))
130+ .append ("coll" , new BsonString (namespace .getCollectionName ()));
131+ }
132+
100133 /**
101134 * Returns the resumeToken
102135 *
@@ -109,10 +142,55 @@ public BsonDocument getResumeToken() {
109142 /**
110143 * Returns the namespace
111144 *
112- * @return the namespace
145+ * The invalidate operation type does include a MongoNamespace in the ChangeStreamDocument response. The
146+ * dropDatabase operation type includes a MongoNamespace, but does not include a collection name as part
147+ * of the namespace.
148+ *
149+ * @return the namespace. If the namespaceDocument is null or if it is missing either the 'db' or 'coll' keys,
150+ * then this will return null.
113151 */
152+ @ BsonIgnore @ Nullable
114153 public MongoNamespace getNamespace () {
115- return namespace ;
154+ if (namespaceDocument == null ) {
155+ return null ;
156+ }
157+ if (!namespaceDocument .containsKey ("db" ) || !namespaceDocument .containsKey ("coll" )) {
158+ return null ;
159+ }
160+
161+ return new MongoNamespace (namespaceDocument .getString ("db" ).getValue (), namespaceDocument .getString ("coll" ).getValue ());
162+ }
163+
164+ /**
165+ * Returns the namespaceDocument
166+ *
167+ * The namespaceDocument is a BsonDocument containing the values associated with a MongoNamespace. The
168+ * 'db' key refers to the database name and the 'coll' key refers to the collection name.
169+ *
170+ * @return the namespaceDocument
171+ * @since 3.8
172+ */
173+ @ BsonProperty ("ns" )
174+ public BsonDocument getNamespaceDocument () {
175+ return namespaceDocument ;
176+ }
177+
178+ /**
179+ * Returns the database name
180+ *
181+ * @return the databaseName. If the namespaceDocument is null or if it is missing the 'db' key, then this will
182+ * return null.
183+ * @since 3.8
184+ */
185+ @ BsonIgnore @ Nullable
186+ public String getDatabaseName () {
187+ if (namespaceDocument == null ) {
188+ return null ;
189+ }
190+ if (!namespaceDocument .containsKey ("db" )) {
191+ return null ;
192+ }
193+ return namespaceDocument .getString ("db" ).getValue ();
116194 }
117195
118196 /**
@@ -196,7 +274,7 @@ public boolean equals(final Object o) {
196274 if (resumeToken != null ? !resumeToken .equals (that .resumeToken ) : that .resumeToken != null ) {
197275 return false ;
198276 }
199- if (namespace != null ? !namespace .equals (that .namespace ) : that .namespace != null ) {
277+ if (namespaceDocument != null ? !namespaceDocument .equals (that .namespaceDocument ) : that .namespaceDocument != null ) {
200278 return false ;
201279 }
202280 if (fullDocument != null ? !fullDocument .equals (that .fullDocument ) : that .fullDocument != null ) {
@@ -221,7 +299,7 @@ public boolean equals(final Object o) {
221299 @ Override
222300 public int hashCode () {
223301 int result = resumeToken != null ? resumeToken .hashCode () : 0 ;
224- result = 31 * result + (namespace != null ? namespace .hashCode () : 0 );
302+ result = 31 * result + (namespaceDocument != null ? namespaceDocument .hashCode () : 0 );
225303 result = 31 * result + (fullDocument != null ? fullDocument .hashCode () : 0 );
226304 result = 31 * result + (documentKey != null ? documentKey .hashCode () : 0 );
227305 result = 31 * result + (clusterTime != null ? clusterTime .hashCode () : 0 );
@@ -234,7 +312,7 @@ public int hashCode() {
234312 public String toString () {
235313 return "ChangeStreamDocument{"
236314 + "resumeToken=" + resumeToken
237- + ", namespace=" + namespace
315+ + ", namespace=" + getNamespace ()
238316 + ", fullDocument=" + fullDocument
239317 + ", documentKey=" + documentKey
240318 + ", clusterTime=" + clusterTime
0 commit comments