Skip to main content
Version: 6.4

publicObjectId

A class representation of the BSON ObjectId type.

Hierarchy

  • BSONValue
    • ObjectId

Index

Constructors

constructor

  • Create ObjectId from a number.

    @deprecated

    Instead, use static createFromTime() to set a numeric value for the new ObjectId.


    Parameters

    • inputId: number

      A number.

    Returns ObjectId

Properties

staticcacheHexString

cacheHexString: boolean

Accessors

_bsontype

  • get _bsontype(): ObjectId
  • Returns ObjectId

id

  • get id(): Uint8Array
  • The ObjectId bytes


    Returns Uint8Array

Methods

equals

  • equals(otherId: undefined | null | string | ObjectId | ObjectIdLike): boolean
  • Compares the equality of this ObjectId with otherID.


    Parameters

    • otherId: undefined | null | string | ObjectId | ObjectIdLike

      ObjectId instance to compare against.

    Returns boolean

getTimestamp

  • getTimestamp(): Date
  • Returns the generation date (accurate up to the second) that this ID was generated.


    Returns Date

inspect

  • inspect(depth?: number, options?: unknown, inspect?: InspectFn): string
  • Converts to a string representation of this Id.


    Parameters

    • optionaldepth: number
    • optionaloptions: unknown
    • optionalinspect: InspectFn

    Returns string

    return the 24 character hex string representation.

toHexString

  • toHexString(): string
  • Returns the ObjectId id as a 24 lowercase character hex string representation


    Returns string

toJSON

  • toJSON(): string
  • Converts to its JSON the 24 character hex string representation.


    Returns string

toString

  • toString(encoding?: hex | base64): string
  • Converts the id into a 24 character hex string for printing, unless encoding is provided.


    Parameters

    • optionalencoding: hex | base64

      hex or base64

    Returns string

staticcreateFromBase64

  • createFromBase64(base64: string): ObjectId
  • Creates an ObjectId instance from a base64 string


    Parameters

    • base64: string

    Returns ObjectId

staticcreateFromHexString

  • createFromHexString(hexString: string): ObjectId
  • Creates an ObjectId from a hex string representation of an ObjectId.


    Parameters

    • hexString: string

      create a ObjectId from a passed in 24 character hexstring.

    Returns ObjectId

staticcreateFromTime

  • Creates an ObjectId from a second based number, with the rest of the ObjectId zeroed out. Used for comparisons or sorting the ObjectId.


    Parameters

    • time: number

      an integer number representing a number of seconds.

    Returns ObjectId

staticgenerate

  • generate(time?: number): Uint8Array
  • Generate a 12 byte id buffer used in ObjectId's


    Parameters

    • optionaltime: number

      pass in a second based timestamp.

    Returns Uint8Array

staticisValid

  • isValid(id: string | number | Uint8Array | ObjectId | ObjectIdLike): boolean
  • Checks if a value can be used to create a valid bson ObjectId


    Parameters

    • id: string | number | Uint8Array | ObjectId | ObjectIdLike

      any JS value

    Returns boolean