i dont know how to insert UUID into MongoDB properly.
For example:
db.users.insert({ uuid: UUID(buffer) })
Buffer must be 32 hex string and MongoDB natively dont create UUID, only stores this? I'm right? But where i create the random UUID, e.g through Node.js middleware i have a random UUID 11986aba-7c5a-4626-b354-80be23c25516, so its a 36 length string and now i cant place it into UUID(buffer)
. What i should doing with this UUID generated from node.js uuid module? And how later read this inserted values from MongoDB as a normal uuid string?
BSON, hence MongoDB support the UUID type. From the Mongo Shell, you can use the
UUID()
constructor to convert from a 32 hex-digits string to an UUID internal representation.From node.js, using the
node-uuid
module you can easily generate v1 or v4 UUID and store them in a buffer object:You can then convert that to type 4 binary
SUBTYPE_UUID
(or the deprecated type 3SUBTYPE_UUID_OLD
):From the Mongo Shell, you get back that value as:
Please note, the Mongo Shell
UUID()
function will produce a type 3SUBTYPE_UUID_OLD
binary: