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
Newsletter
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
  • Node.js Microservices Architecture: A Complete Guide

Trending

  • Google Cloud KMS vs. AWS KMS: Java Encryption Guide
  • Apache Phoenix: Global Secondary Indexes With Tunable Consistency
  • MQTT, Kafka, NATS, or Zenoh? Choosing Edge-to-Cloud Messaging by Traffic Pattern
  • Why Kotlin or Java Is the Wrong Question For Enterprises

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Sep. 12, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
21.1K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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
  • Node.js Microservices Architecture: A Complete Guide

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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