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 collections = await db.createCollection('myCollection'. {
  defaultId: {
    type: 'uuidv7',
  },
});

await collections.insertOne({ type: 'Jomsvikings' });

const doc = await collections.findOne({ type: 'Jomsvikings' });

// 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 collections.insertOne({ _id: UUID.v4(), car: 'toon' });

const doc = await collections.findOne({ car: 'toon' });

// 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

Implements

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.