Get a BinData UUID from MongoDB as a string
Codemia Knowledge Hub ·
MongoDB stores UUIDs as BinData, usually subtype 4. To get one back as a string, convert the binary value to hex and format it into the usual dashed UUID layout, either in the Mongo shell or in your driver code (for example with uuid.stringify in Node.js).
BinData and UUIDs in MongoDB
BinData is a BSON type for binary data, such as JSON web tokens, encrypted information, or data encoded in formats like UUIDs. It supports several subtypes, including general binary data, functions and UUIDs.
A UUID (Universally Unique Identifier) is a 128-bit label used across systems to identify information uniquely. MongoDB can store it as a BinData subtype, which is more efficient for performance and storage than a string representation. Systems that expect strings, though, need it converted.
A UUID stored as BinData looks like this:
The BinData type "4" marks a standard UUID.
Converting in the Mongo shell
In the MongoDB shell you can convert with JavaScript:
Converting with the Node.js driver
With a driver the approach is the same, with slightly different syntax:
Trade-offs of storing BinData and converting
- Storage and processing:
BinDatais storage-efficient, but converting to strings costs processing time. Do the conversion where its performance impact is smallest. - Compatibility: UUIDs are common in system integrations, and a string form is needed for platforms that don't understand Mongo's binary UUID format.
- Security: treat converted values with the same care as any sensitive data, and transport them over secure, encrypted connections.
| Feature | BinData Storage | String Representation |
| Efficiency | High storage efficiency | Slower upon conversion |
| UUID Format | BinData subtype "4" (UUID) | Alphanumeric with dashes |
| Usage Compatibility | Direct in MongoDB | Required for external applications |
| Performance | Faster native operations | Conversion can be resource-intensive |
| Storage Concerns | Minimal space usage | Requires extra computation for conversion |
| Security Practices | Keep data secure at rest | Ensure secure transmission and usage |
Related reading
- Get boolean from database using Android and SQLite
- Get current AUTO_INCREMENT value for any table
- Get first row for each partition key in Cassandra
- Get Insert Statement for existing row in MySQL
- Get list of all tables in Oracle?
- Get minimum Euclidean distance between a given vector and vectors in the database
- Get output parameter value in ADO.NET
- Get query from java.sql.PreparedStatement
System design interview problems on this topic
- Design a Distributed OLTP Database: solution and walkthrough
- Design a Wide Column Database: solution and walkthrough
- Design a Key Value Store: solution and walkthrough
- Design a Web Cache: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.