Skip to content

Instantly share code, notes, and snippets.

@gerbrand
Created July 20, 2018 11:56
Embed
What would you like to do?
Converting an UUID to an MongoDB ObjectId by discarding a few insignificant bits
object UUIDToObjectId {
def uuidToObjectId(uuid: UUID): ObjectId = {
import java.util.UUID
import org.bson.types.ObjectId
val b = java.nio.ByteBuffer.allocate(12)
b.putLong(uuid.getMostSignificantBits)
b.putInt((uuid.getLeastSignificantBits >> 16).intValue)
new ObjectId(b.array())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment