Docs Menu

Docs HomeRealm

UUID - Node.js SDK

On this page

  • Overview
  • Usage

New in version 10.5.0.

UUID (Universal Unique Identifier) is a 16-byte unique value. You can use UUID as an identifier for objects. UUID is indexable and you can use it as a primary key.


Using UUID Instead of ObjectId

In general, you can use UUID for any fields that function as a unique identifier. Using UUID might be particularly useful if you are migrating data not stored in MongoDB since it is likely that your object's unique identifiers are already of a UUID type. Alternatively, using ObjectId might be useful for a collection of data that already exists in MongoDB.

To define a property as a UUID, set its type to the string "uuid" in your object model. Create a Realm object within a write transaction. To set any unique identifier properties of your object to a random value, call new UUID(). Alternatively, pass a string to new UUID() to set the unique identifier property to a specific value.

const { UUID } = Realm.BSON;
const ProfileSchema = {
name: "Profile",
primaryKey: "_id",
properties: {
_id: "uuid",
name: "string",
const realm = await{
schema: [ProfileSchema],
realm.write(() => {
realm.create("Profile", {
name: "John Doe.",
_id: new UUID(), // create a _id with a randomly generated UUID
realm.create("Profile", {
name: "Tim Doe.",
_id: new UUID("882dd631-bc6e-4e0e-a9e8-f07b685fec8c"), // create a _id with a specific UUID value
←  Mixed - Node.js SDKEmbedded Objects - Node.js SDK →

On this page

Share Feedback
© 2023 MongoDB, Inc.


  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2023 MongoDB, Inc.