Represents a UUID that can be used as an _id in the DataAPI.

Provides methods for creating v4 and v7 UUIDs, and for parsing timestamps from v7 UUIDs.

Example

const collection = await db.createCollection('myCollection'. {
  defaultId: {
    type: 'uuidv7',
  },
});

await collection.insertOne({ album: 'Jomsviking' });

const doc = await collection.findOne({ album: 'Jomsviking' });

// Prints the UUID of the document
console.log(doc._id.toString());

// Prints the timestamp when the document was created (server time)
console.log(doc._id.getTimestamp());

Example

await collection.insertOne({ _id: UUID.v4(), album: 'Berserker' });

const doc = await collection.findOne({ album: 'Berserker' });

// Prints the UUID of the document
console.log(doc._id.toString());

// Undefined, as the document was created with a v4 UUID
console.log(doc._id.getTimestamp());

See

ObjectId

Constructors

  • Creates a new UUID instance.

    Use UUID.v4() or UUID.v7() to generate random new UUIDs.

    Parameters

    • uuid: string

      The UUID string.

    • validate: boolean = true

      Whether to validate the UUID string. Defaults to true.

    Returns UUID

Properties

_raw: string
version: number

The version of the UUID.

Methods

  • Compares this UUID to another UUID.

    The other UUID can be a UUID instance or a string.

    A UUID is considered equal to another UUID if their lowercase string representations are equal.

    Parameters

    • other: unknown

      The UUID to compare to.

    Returns boolean

    true if the UUIDs are equal, false otherwise.

  • Returns the timestamp of a v7 UUID. If the UUID is not a v7 UUID, this method returns undefined.

    Returns undefined | Date

    The timestamp of the UUID, or undefined if the UUID is not a v7 UUID.

  • Converts the UUID to a JSON representation.

    Serializes to { $uuid: 'uuid' }.

    Returns {
        $uuid: string;
    }

    • $uuid: string
  • Returns the string representation of the UUID in lowercase.

    Returns string