Created
July 20, 2018 11:56
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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