You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MongoDB Compass中指定UUID与Base64二进制互转算法求助

Solving MongoDB Compass UUID-to-Base64 Conversion (and Vice Versa)

I totally get the frustration—MongoDB Compass displaying UUIDs as raw binary makes searching and verifying them a huge pain. Let’s fix this with two simple, universal algorithms that work regardless of MongoDB binary subtype (no reliance on 0x04 here) and handle your exact example perfectly.

UUID to Base64 Conversion

The core idea here is to strip the UUID’s hyphens, convert the raw hex string to bytes, then encode those bytes to Base64. No fancy subtype handling needed—just direct conversion.

JavaScript Example

function uuidToBase64(uuid) {
  // Remove hyphens from the UUID string
  const cleanHex = uuid.replace(/-/g, '');
  // Convert hex pairs to a Uint8Array of bytes
  const byteArray = new Uint8Array(cleanHex.length / 2);
  for (let i = 0; i < cleanHex.length; i += 2) {
    byteArray[i / 2] = parseInt(cleanHex.slice(i, i + 2), 16);
  }
  // Encode bytes to Base64
  return btoa(String.fromCharCode(...byteArray));
}

// Test with your sample UUID
const sampleUuid = '87c8bc80-4fce-11e9-8647-1563bd873d93';
console.log(uuidToBase64(sampleUuid)); // Output: 6RHOT4C8yIeTPYe9YxVHhg==

Python Example

import base64

def uuid_to_base64(uuid_str):
    # Strip hyphens and convert hex to bytes
    hex_bytes = bytes.fromhex(uuid_str.replace('-', ''))
    # Encode bytes to Base64 string
    return base64.b64encode(hex_bytes).decode('utf-8')

# Test with your sample UUID
sample_uuid = '87c8bc80-4fce-11e9-8647-1563bd873d93'
print(uuid_to_base64(sample_uuid)) # Output: 6RHOT4C8yIeTPYe9YxVHhg==

Base64 to UUID Conversion

Reverse the process: decode the Base64 string back to bytes, convert those bytes to a hex string, then reinsert hyphens to match standard UUID format.

JavaScript Example

function base64ToUuid(base64Str) {
  // Decode Base64 to binary string
  const binaryStr = atob(base64Str);
  // Convert binary string to Uint8Array
  const byteArray = new Uint8Array(binaryStr.length);
  for (let i = 0; i < binaryStr.length; i++) {
    byteArray[i] = binaryStr.charCodeAt(i);
  }
  // Convert bytes to hex string and format as UUID
  const hexStr = Array.from(byteArray, byte => byte.toString(16).padStart(2, '0')).join('');
  return hexStr.replace(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/, '$1-$2-$3-$4-$5');
}

// Test with your sample Base64
const sampleBase64 = '6RHOT4C8yIeTPYe9YxVHhg==';
console.log(base64ToUuid(sampleBase64)); // Output: 87c8bc80-4fce-11e9-8647-1563bd873d93

Python Example

import base64

def base64_to_uuid(base64_str):
    # Decode Base64 to bytes
    byte_data = base64.b64decode(base64_str)
    # Convert bytes to hex string
    hex_str = byte_data.hex()
    # Format hex string into UUID format
    return f"{hex_str[:8]}-{hex_str[8:12]}-{hex_str[12:16]}-{hex_str[16:20]}-{hex_str[20:]}"

# Test with your sample Base64
sample_base64 = '6RHOT4C8yIeTPYe9YxVHhg=='
print(base64_to_uuid(sample_base64)) # Output: 87c8bc80-4fce-11e9-8647-1563bd873d93

Why This Works Where Others Failed

Most pre-built MongoDB UUID tools assume you’re working with subtype 0x04 (which enforces RFC 4122 byte ordering for UUIDs). But your scenario doesn’t use that subtype, so we skip the byte reordering entirely—just raw hex ↔ bytes ↔ Base64 conversion. This also works reliably for standard 16-byte UUIDs, avoiding the issues with short binary lengths you mentioned.

内容的提问来源于stack exchange,提问作者youngDev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 06:47:00