Skip to content

Instantly share code, notes, and snippets.

@gerbrand
Created July 20, 2018 11:56
Show Gist options
  • Save gerbrand/bc27832755e37ea43b001e216797fbb2 to your computer and use it in GitHub Desktop.
Save gerbrand/bc27832755e37ea43b001e216797fbb2 to your computer and use it in GitHub Desktop.
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