async scala driver: how to encode an UUID in BSON

239 views
Skip to first unread message

Yann Simon

unread,
Dec 30, 2015, 6:01:44 AM12/30/15
to mongodb-user
Hi,


I have not found out how to encode a java.util.UUID.
By looking into the code of the different drivers, I found out I could use BsonBinary with the right BsonBinarySubType. But then I have to convert the UUID to an array of bytes myself.

Thanks for any pointer,
Yann

Jeff Yemin

unread,
Dec 30, 2015, 10:06:35 AM12/30/15
to mongodb-user
Hi Yann,

It's not elegant, but here's one way to do it: https://gist.github.com/jyemin/45996b72977c4453ca7f.

Make sure to choose an appropriate UuidRepresentation based upon the needs of your particular application.


Regards,
Jeff

Yann Simon

unread,
Dec 30, 2015, 10:17:22 AM12/30/15
to mongodb-user
Thank for the feedback Jeff!

I will try to incorporate that. For the moment, I created BasicDBObject and converted it with BsonDocumentWrapper.
It is not elegant neither ;)

Do you think it'd make sense to add a BsonUUID to encapsulate this logic?

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/fd676556-6350-4d0c-90cb-05133e35cb29%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jeff Yemin

unread,
Dec 30, 2015, 11:10:52 AM12/30/15
to mongodb-user
I haven't considered it, but please feel free to open a ticket for that in the JAVA project in Jira.


Regards,
Jeff

Damir Vandic

unread,
Jan 14, 2016, 4:47:55 PM1/14/16
to mongodb-user
I believe this seemed to work for me:

import java.nio.ByteBuffer
import java.util.UUID

import org.bson.{BsonBinary, BsonBinarySubType}
import org.mongodb.scala.bson
import org.mongodb.scala.bson.BsonTransformer

object Test {
implicit val uuidTransformer: BsonTransformer[UUID] = new BsonTransformer[UUID] {
override def apply(value: UUID): bson.BsonValue = {
val bb = ByteBuffer.wrap(new Array[Byte](16))
bb.putLong(value.getMostSignificantBits)
bb.putLong(value.getLeastSignificantBits)
new BsonBinary(BsonBinarySubType.UUID_STANDARD, bb.array)
}
}
}

Yann Simon

unread,
Jan 18, 2016, 5:53:22 AM1/18/16
to mongodb-user
Thanks Damir, your approach works as well!

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
Reply all
Reply to author
Forward
0 new messages