Skip to content

Instantly share code, notes, and snippets.

@knalli
Last active September 2, 2020 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knalli/d09697653a09b9877ee8dcf5347f957e to your computer and use it in GitHub Desktop.
Save knalli/d09697653a09b9877ee8dcf5347f957e to your computer and use it in GitHub Desktop.
Mongo UUID Type 3 <-> Type 4 Conversion

This solves the issue that MongoDB documents are stored under and read with different UUID formats between different environments, for example Java drivers and the Mongo Shell.

Prerequistes

  1. Download https://github.com/mongodb/mongo-csharp-driver/blob/750d021a0ea5fcc6ade34b4a968fdbe16f3d2377/uuidhelpers.js
  2. Load the script into the Mongo Shell, i.e. mongo --shell uuidhelpers.js

Situation: 36-chars-UUID -> BinData-Type-3

JUUID("a635b4cf-0d53-4780-8b10-0f27ec2197ff")

Situation: BinData-Type-3 -> 36-chars-UUID

BinData(3, 'gEdTDc+0Nab/lyHsJw8Qiw==').toJUUID()
#!/bin/bash
binid=$1
if [ -z "$binid" ]; then echo "missing binid"; exit 1; fi
echo "print(BinData(3, '$binid').toJUUID())" | mongo --shell uuidhelpers.js --quiet
exit $?
#!/bin/bash
uuid=$1
if [ -z "$uuid" ]; then echo "missing uuid"; exit 1; fi
echo "print(JUUID('$uuid'))" | mongo --shell uuidhelpers.js --quiet
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment