DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Mastering Full-Stack Development: A Comprehensive Beginner’s Guide to the MERN Stack
  • How To Perform Data Migration in MongoDB Using Node.js
  • Image Storage: Building a Node.js API With MongoDB
  • Stop Writing Dialect-Specific SQL: A Unified Query Builder for Node.js

Trending

  • OpenAPI From Code With Spring and Java: A Recipe for Your CI
  • No More Cheap Claude: 4 First Principles of Token Economics in 2026
  • Why We Chose Iceberg Over Delta After Evaluating Both at Scale
  • LLM Integration in Enterprise Applications: A Practical Guide
  1. DZone
  2. Data Engineering
  3. Databases
  4. UUIDs With MongoDB and Node.js

UUIDs With MongoDB and Node.js

Let's take a quick look at UUIDs with MongoDB and Node.js and also explore an example of how to use uuid-mongodb to query MongoDB.

By 
Carmine DiMascio user avatar
Carmine DiMascio
·
Sep. 12, 18 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
20.9K Views

Join the DZone community and get the full member experience.

Join For Free

My team uses MongoDB quite heavily in a variety of our service backends. Some of these services interface with one another. For example, one service might request a resource by ID from another service and store that ID as a reference. To identify resources, we use UUIDs quite extensively due to their ubiquitous nature and general support.

MongoDB, like most databases, provide support for UUIDs, however using UUIDs with MongoDB and Node.js is not as trivial as one might expect (Note that I’m not going to touch on UUID complexities in multi-platform/multi-language scenarios).

First off, for performance reasons, it is highly beneficial to use BSON UUIDs, rather than UUID strings. BSON UUIDs represent UUIDs as binary data. This has the great advantage of providing improved overall performance, particularly with respect to queries.

Secondly, in order to use BSON UUIDs, a developer must wrangle with the pain of converting to and from Binary UUIDs and their string counterparts.

Thirdly, if using Node.js, you’ll find that neither the Native MongoDB Driver nor (the popular) Mongoose library provide a simple interface for BSON UUIDs. Instead, Node.js developers must work with generic Binary type. This type is rather general purpose and unfortunately, makes it relatively complicated to work with BSON UUIDs. Because of this, some developers give up and revert to strings. This is not recommended

All in all, my team and I found ourselves in this similar, painful place. Fortunately, we attempted to ease the burden for ourselves and hopefully for all of you. We developed a small Node.js library called uuid-mongodb that enables the straightforward usage of BSON UUIDs with Node.js. The library mitigates conversion pains and generates UUID’s that are 100% compatible with the Native MongoDB Driver. It is also compatible with Mongoose:

uuid-mongodb

Let’s see an example of how to use uuid-mongodb to query MongoDB:

In this example, we convert a UUID string to a BSON UUID and use it in a query:

In the next example, we execute a MongoDB query that returns a document containing a BSON UUID and convert it to a string:

All in all, uuid-mongodb aims to ease the burden of using BSON UUIDs with MongoDB. We hope you find it useful.

For those looking to contribute, provide feedback, or just use it in your Node.js application, go here.

MongoDB Node.js

Opinions expressed by DZone contributors are their own.

Related

  • Mastering Full-Stack Development: A Comprehensive Beginner’s Guide to the MERN Stack
  • How To Perform Data Migration in MongoDB Using Node.js
  • Image Storage: Building a Node.js API With MongoDB
  • Stop Writing Dialect-Specific SQL: A Unified Query Builder for Node.js

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook