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