My journey begins with me trying to configure Java driver of MongoDB to use UUID v4 instead of Legacy UUID v3 which is set by default.
I've found this solution here https://groups.google.com/forum/#!msg/mongodb-user/ZJKQpMpCMU4/dW5ATHTcAvgJ which works.
But as he states:
Note that when using the legacy API the codec registry is ignored, so this will not use the overridden UUIDCodec
it doesn't work with my MongoRepositoy
.
This is my actual configuration:
@Bean
public MongoDbFactory mongoDbFactory() throws Exception {
ServerAddress server = new ServerAddress(host,port);
MongoClientOptions.Builder mcoBuilder = MongoClientOptions.builder();
CodecRegistry codecRegistry = fromRegistries(fromCodecs(new UuidCodec(UuidRepresentation.STANDARD)),
MongoClient.getDefaultCodecRegistry());
mcoBuilder.codecRegistry(codecRegistry).build();
MongoClientOptions options = mcoBuilder.build();
MongoClient mongoClient = new MongoClient(server,options);
return new SimpleMongoDbFactory(mongoClient, mongoDataBase);
}
@Bean
public MongoTemplate mongoTemplate() throws Exception {
MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory());
return mongoTemplate;
}
If I do:
mongoClient.getDatabase(mongoDataBase).getCollection("test")
.insertOne(new Document("_id",UUID.randomUUID()));
I get:
{ "_id" : BinData(4,"f0u8ig4TS6KaJGK93xmvNw==") }
Otherwise:
mongoTemplate.getCollection("test")
.insert(new BasicDBObject("_id", UUID.randomUUID()));
result on:
{ "_id" : BinData(3,"mUX4PTPBJo6bIjPufHf0vg==") }
I know MongoRepository
uses MongoTemplate
, although I've set the instance to use MongoClient
and not the old Mongo
, still not working. Is there any solution?
The MongoRepository provides save() and saveAll() methods to update the entities. If entities are already available in collection in Mongo database, then they will be updated otherwise they will be inserted as new entities. The save() method updates one entity at a time and returns the updated entity.
Interface MongoDbFactory. Deprecated. since 3.0, use MongoDatabaseFactory instead.
We can even utilize both of them in our programming practice as per our need and for performance enhancements. Moreover, MongoTemplate can offer an easier step to write a complex query than MongoRepository. Even some people in the industry also find that MongoTemplate is a better choice to write a complex query easily.
To connect to MongoDB Atlas, we specify the connection string in the application. properties file in the src/main/resources folder. The connection string for a cluster can be found in the Atlas UI. There is no need to write connection-related code in any other file.
MongoClient
extends Mongo
, which has reference to legacy api DB
class via getDB()
. Although you've registered the new UUID codec with MongoClient
which can only be used when you use getDatabase()
to get MongoDatabase
which spring mongo template current version doesn't and uses getDB()
. So your changes to registry are never used.
Spring MongoDB 2.0.0 versions has been updated to use new java driver api. So your changes should work as expected against 2.0.0 version.
http://docs.spring.io/spring-data/data-mongo/docs/2.0.0.M4/reference/html/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With