{
    "mode": "perldoc",
    "parameter": "BSON::OID",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/BSON%3A%3AOID/json",
    "generated": "2026-06-12T13:49:18Z",
    "synopsis": "use BSON::Types ':all';\nmy $oid  = bsonoid();\nmy $oid  = bsonoid->fromepoch(1467543496, 0); # for queries only\nmy $bytes = $oid->oid;\nmy $hex   = $oid->hex;",
    "sections": {
        "NAME": {
            "content": "BSON::OID - BSON type wrapper for Object IDs\n",
            "subsections": []
        },
        "VERSION": {
            "content": "version v1.12.2\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use BSON::Types ':all';\n\nmy $oid  = bsonoid();\nmy $oid  = bsonoid->fromepoch(1467543496, 0); # for queries only\n\nmy $bytes = $oid->oid;\nmy $hex   = $oid->hex;\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "This module provides a wrapper around a BSON Object ID\n<https://docs.mongodb.com/manual/reference/method/ObjectId/>.\n",
            "subsections": []
        },
        "ATTRIBUTES": {
            "content": "oid\nA 12-byte (packed) Object ID (OID) string. If not provided, a new OID will be generated.\n",
            "subsections": []
        },
        "METHODS": {
            "content": "new\nmy $oid = BSON::OID->new;\n\nmy $oid = BSON::OID->new( oid => $twelvebytes );\n\nThis is the preferred way to generate an OID. Without arguments, a unique OID will be generated.\nWith a 12-byte string, an object can be created around an existing OID byte-string.\n\nfromepoch\n# generate a new OID\n\nmy $oid = BSON::OID->fromepoch( $epoch, 0); # other bytes zeroed\nmy $oid = BSON::OID->fromepoch( $epoch, $eightmorebytes );\n\n# reset an existing OID\n\n$oid->fromepoch( $newepoch, 0 );\n$oid->fromepoch( $newepoch, $eightmorebytes );\n\nWarning! You should not rely on this method for a source of unique IDs. Use this method for\nquery boundaries, only.\n\nAn OID is a twelve-byte string. Typically, the first four bytes represent integer seconds since\nthe Unix epoch in big-endian format. The remaining bytes ensure uniqueness.\n\nWith this method, the first argument to this method is an epoch time (in integer seconds). The\nsecond argument is the remaining eight-bytes to append to the string.\n\nWhen called as a class method, it returns a new BSON::OID object. When called as an object\nmethod, it mutates the existing internal OID value.\n\nAs a special case, if the second argument is defined and zero (\"0\"), then the remaining bytes\nwill be zeroed.\n\nmy $oid = BSON::OID->fromepoch(1467545180, 0);\n\nThis is particularly useful when looking for documents by their insertion date: you can simply\nlook for OIDs which are greater or lower than the one generated with this method.\n\nFor backwards compatibility with Mango, if called without a second argument, the method\ngenerates the remainder of the fields \"like usual\". This is equivalent to calling\n\"BSON::OID->new\" and replacing the first four bytes with the packed epoch value.\n\n# UNSAFE: don't do this unless you have to\n\nmy $oid = BSON::OID->fromepoch(1467545180);\n\nIf you insist on creating a unique OID with \"fromepoch\", set the remaining eight bytes in a way\nthat guarantees thread-safe uniqueness, such as from a reliable source of randomness (see\nCrypt::URandom).\n\nuse Crypt::Random 'urandom';\nmy $oid = BSON::OID->fromepoch(1467545180, urandom(8));\n\nhex\nReturns the \"oid\" attributes as 24-byte hexadecimal value\n\ngettime\nReturns a number corresponding to the portion of the \"oid\" value that represents seconds since\nthe epoch.\n\nTOJSON\nReturns a string for this OID, with the OID given as 24 hex digits.\n\nIf the \"BSONEXTJSON\" option is true, it will instead be compatible with MongoDB's extended JSON\n<https://github.com/mongodb/specifications/blob/master/source/extended-json.rst> format, which\nrepresents it as a document as follows:\n\n{\"$oid\" : \"012345678901234567890123\"}\n",
            "subsections": []
        },
        "OVERLOAD": {
            "content": "The string operator is overloaded so any string operations will actually use the 24-character\nhex value of the OID. Fallback overloading is enabled.\n\nBoth numeric comparison (\"<=>\") and string comparison (\"cmp\") are overloaded to do string\ncomparison of the 24-character hex value of the OID. If used with a non-BSON::OID object, be\nsure to provide a 24-character hex string or the results are undefined.\n",
            "subsections": []
        },
        "THREADS": {
            "content": "This module is thread safe.\n",
            "subsections": []
        },
        "AUTHORS": {
            "content": "*   David Golden <david@mongodb.com>\n\n*   Stefan G. <minimalist@lavabit.com>\n",
            "subsections": []
        },
        "COPYRIGHT AND LICENSE": {
            "content": "This software is Copyright (c) 2020 by Stefan G. and MongoDB, Inc.\n\nThis is free software, licensed under:\n\nThe Apache License, Version 2.0, January 2004\n",
            "subsections": []
        }
    },
    "summary": "BSON::OID - BSON type wrapper for Object IDs",
    "flags": [],
    "examples": [],
    "see_also": []
}