How to set UuidRepresentation to Standard with Java driver?

2,175 views
Skip to first unread message

Alexander Bagerman

unread,
Mar 4, 2015, 6:59:04 PM3/4/15
to mongod...@googlegroups.com
Hi,
I am starting a new project and would my UUIDs to get saved in UuidRepresentation.Standard representation rather as default UuidRepresentation.JAVA_LEGACY. I saw that C# driver allows you to submit it as a part of URI string (in options) but not the Java driver. 

What is the proper way of doing it?

Regards,
Alex

Jeff Yemin

unread,
Mar 4, 2015, 9:20:54 PM3/4/15
to mongod...@googlegroups.com
This is only supported starting with the 3.0 Java driver (now in beta).  It's not supported as part of the URI; it's configurable only in code or equivalent configuration (Spring, Guice, etc):

import static org.bson.codecs.configuration.CodecRegistryHelper.fromCodec;
import static org.bson.codecs.configuration.CodecRegistryHelper.fromRegistries;
...
MongoClientOptions.Builder builder = MongoClientOptions.builder();
builder.codecRegistry(fromRegistries(fromCodec(new UuidCodec(UuidRepresentation.STANDARD)),
MongoClient.getDefaultCodecRegistry()));
MongoClientOptions options = builder.build();

Regards,
Jeff

Alexander Bagerman

unread,
Mar 5, 2015, 1:40:16 PM3/5/15
to mongod...@googlegroups.com
Jeff,
Thanks, I tried that and can see org.bson.codecs.configuration.CompoundCodecRegistry  with "firstCodecRegistry" set to SingleCodecRegistry with expected UuidCodec. 

Unfortunately even though execution goes through CompoundCodecRegistry constructor "public <T> Codec<T> get(final Class<T> clazz)" never gets called :-(. As a result added codec is never used.

I am using the following version of the driver:
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.0.0-beta3</version>

And use this code to configure MongoClient:
ServerAddress serverAddress = new ServerAddress(getMongoHost(), getMongoPortNumber());
MongoCredential mongoCredential = MongoCredential.createMongoCRCredential(getMongoUser(), getDatabaseName(),
getMongoPassword().toCharArray());
MongoClientOptions.Builder builder = MongoClientOptions.builder();
builder.codecRegistry(fromRegistries(fromCodec(new UuidCodec(UuidRepresentation.STANDARD)),
                                    MongoClient.getDefaultCodecRegistry()));
MongoClientOptions options = builder.build();
MongoClient client = new MongoClient(serverAddress, Arrays.asList(mongoCredential), options);

Any ideas would be appreciated
Alex 

Jeff Yemin

unread,
Mar 5, 2015, 4:38:44 PM3/5/15
to mongod...@googlegroups.com
Hi Alexander,

Thank you. You found a significant bug, which I filed as https://jira.mongodb.org/browse/JAVA-1675.  I have a fix in code review at https://github.com/jyemin/mongo-java-driver/tree/j1675.  Using that branch, this now works as you would expect:

MongoClientOptions.Builder builder = MongoClientOptions.builder();
CodecRegistry codecRegistry = fromRegistries(fromCodec(new UuidCodec(UuidRepresentation.STANDARD)),
MongoClient.getDefaultCodecRegistry());

builder.codecRegistry(codecRegistry);
MongoClientOptions options = builder.build();
MongoClient client = new MongoClient(new ServerAddress(), options);

client.getDatabase("test").getCollection("test").insertOne(new Document("_id", UUID.randomUUID()));


Note that when using the legacy API the codec registry is ignored, so this will not use the overridden UUIDCodec:

     client.getDB("test").getCollection("test").insert(new BasicDBObject("_id", UUID.randomUUID()));

It was a deliberate decision to leave the legacy API's behavior as is, as the DBEncoder/DBDecoder behavior on DBCollection is complicated enough as it is.

Thanks again, and regards,
Jeff

Alexander Bagerman

unread,
Mar 5, 2015, 4:56:32 PM3/5/15
to mongod...@googlegroups.com
Jeff,
Is there an analog of BasicDBList that I should start using as well? Some of my UUIDs are buried in arrays of document so i currently have something like this:
BasicDBList l = new BasicDBList();
l.add(new BasicDBObject().append("code": UUID.randomUUID()));
collection.insert(new BasicDBObject().append("list": l))

BTW, how can I add more codec at the same time as setting up my UUID one?

Thanks,
Alex

Jeff Yemin

unread,
Mar 5, 2015, 5:07:41 PM3/5/15
to mongod...@googlegroups.com
You can use ArrayList instead.

Can you give me an example of other codecs that you want to add?  Do none of the other methods in CodecRegistryHelper suit your needs?

Alexander Bagerman

unread,
Mar 5, 2015, 5:17:14 PM3/5/15
to mongod...@googlegroups.com
I am migrating data from JDBC source and would like just pass Date onto the mongo which fails as it can not find java.sql.Date codec. So, I do conversation to java.util.Date every time append it to by BasicDBObject (that I am going to convert to Document)

Jeff Yemin

unread,
Mar 6, 2015, 4:08:46 PM3/6/15
to mongod...@googlegroups.com
OK, I pushed up some changes that should help you.  Check out this gist for what it should look like.

Let me know if it works for you.

Regards,
Jeff

Alexander Bagerman

unread,
Mar 6, 2015, 10:16:16 PM3/6/15
to mongod...@googlegroups.com
Thanks, worked perfectly!
Reply all
Reply to author
Forward
Message has been deleted
0 new messages