|
Currently, the resumeToken for a ChangeStreamIterable is encoded using the default codec registry for the collection. But that can cause an issue because the code registry may be (and is by default) configured with a registry that encodes UUID as type 3 (this goes back to a very early incompatibility between different MongoDB drivers), and the server will error if the resumeToken is anything but type 4.
To work around this, an application can configure a different registry for the MongoCollection used to create the change stream:
CodecRegistry codecRegistry = CodecRegistries.fromRegistries(
|
CodecRegistries.fromCodecs(new UuidCodec(UuidRepresentation.STANDARD)),
|
collection.getCodecRegistry());
|
collection = collection.withCodecRegistry(codecRegistry);
|
|
Document resumeToken = next.get("_id", Document.class);
|
MongoCursor<Document> iterator = collection.watch().resumeAfter(resumeToken).iterator();
|
But this is only safe to do if the actual documents in the watched collection do not contain any type 3 UUIDs. Otherwise, they will be decoded incorrectly in the fullDocument field of the change stream documents and also when re-encoding the resumeToken, as the resumeToken itself contains the _id of the document.
|