Reputation: 155
I upgraded to Spring Boot 2.2.0.RELEASE and wanted to replace the now deprecated AbstractMongoConfiguration with AbstractMongoClientConfiguration. I am using
codecRegistries.add(CodecRegistries.fromCodecs(new UuidCodec(UuidRepresentation.STANDARD)));
to set the UUID Codec in the MongoDB to STANDARD (UUID) instead of Legacy Codec (LUUID). When looking into the database the Codec stays in the legacy format. Anybody else experienced the same problem?
Old implementation (working):
@Override
public MongoClient mongoClient() {
CodecRegistry codecRegistry =
CodecRegistries.fromRegistries(CodecRegistries.fromCodecs(new UuidCodec(UuidRepresentation.STANDARD)),
MongoClient.getDefaultCodecRegistry());
return new MongoClient(new ServerAddress(address, port), MongoClientOptions.builder().codecRegistry(codecRegistry).build());
}
New implementation (not working):
@Override
public MongoClient mongoClient() {
List<CodecRegistry> codecRegistries = new ArrayList<>();
codecRegistries.add(CodecRegistries.fromCodecs(new DocumentCodec()));
codecRegistries.add(CodecRegistries.fromCodecs(new UuidCodec(UuidRepresentation.STANDARD)));
CodecRegistry codecRegistry = CodecRegistries.fromRegistries(codecRegistries);
return MongoClients.create(MongoClientSettings.builder()
.codecRegistry(codecRegistry)
.applyConnectionString(new ConnectionString(connectionString))
.build());
}
I expected the UUID Codec in the database to adjust to Standard Codec but it stays in Legacy Codec.
Upvotes: 9
Views: 2286
Reputation: 1
I write for a visiter to solve the same issue.
append ?uuidRepresentation=STANDARD
to connectionString.
It works for me.
Upvotes: 0
Reputation: 155
I found a solution for the problem. The new UuidCodec(UuidRepresentation.STANDARD)
needs to be at the first position. My Code looks like following:
private static final CodecRegistry CODEC_REGISTRY = CodecRegistries.fromProviders(
Arrays.asList(new UuidCodecProvider(UuidRepresentation.STANDARD),
new ValueCodecProvider(),
new BsonValueCodecProvider(),
new DBRefCodecProvider(),
new DBObjectCodecProvider(),
new DocumentCodecProvider(new DocumentToDBRefTransformer()),
new IterableCodecProvider(new DocumentToDBRefTransformer()),
new MapCodecProvider(new DocumentToDBRefTransformer()),
new GeoJsonCodecProvider(),
new GridFSFileCodecProvider(),
new Jsr310CodecProvider(),
new BsonCodecProvider()));
That behavior is not very nice and it is possibly a bug. Hope this helps some of you.
Upvotes: 4