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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

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
  • While Performing Dependency Selection, I Avoid the Loss Of Sleep From Node.js Libraries' Dangers

Trending

  • How to Convert Between PDF and TIFF in Java
  • Start Coding With Google Cloud Workstations
  • Why I Started Using Dependency Injection in Python
  • The Role of Functional Programming in Modern Software Development
  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.7K 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
  • While Performing Dependency Selection, I Avoid the Loss Of Sleep From Node.js Libraries' Dangers

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!