BSON::OID(3pm)
π BSON::OID β the perl wrapper for Object IDs, bestie edition π
okay so real talk: this is BSON::OID, a User Contributed Perl Documentation page, and it's literally the BSON type wrapper for Object IDs. yes. that's the whole vibe. it's giving "tiny module, big responsibility." π«Ά
β¨ VERSION β¨
version v1.12.2 β she's a specific girl and she knows it π
πΈ SYNOPSIS πΈ
here's the whole thing in action, no cap:
use BSON::Types ':all';
my $oid = bson_oid();
my $oid = bson_oid->from_epoch(1467543496, 0); # for queries only
my $bytes = $oid->oid;
my $hex = $oid->hex;
see how she pulls the raw bytes AND the hex? versatility. main character energy fr. π
π¦ DESCRIPTION π¦
this module is a wrapper around a BSON Object ID β here's the link so you can go read the MongoDB docs yourself:
https://docs.mongodb.com/manual/reference/method/ObjectId/
and that's it. that's the description. short and iconic, as she should be. β¨
π ATTRIBUTES π
oid
a 12-byte (packed) Object ID (OID) string. and if you don't hand her one? she just generates a brand new OID for you. no thoughts, just creation. π
π METHODS π
new
my $oid = BSON::OID->new;
my $oid = BSON::OID->new( oid => $twelve_bytes );
okay bestie, this is the preferred way to generate an OID. like, if you're only gonna remember one thing from this page, it's this one. π
- call it with no arguments β you get a unique OID, freshly generated, just for you π
- call it with a 12-byte string β you get an object wrapped around an OID byte-string that already exists
from_epoch
here come the examples, they're doing the most and honestly? respect:
# generate a new OID
my $oid = BSON::OID->from_epoch( $epoch, 0); # other bytes zeroed
my $oid = BSON::OID->from_epoch( $epoch, $eight_more_bytes );
# reset an existing OID
$oid->from_epoch( $new_epoch, 0 );
$oid->from_epoch( $new_epoch, $eight_more_bytes );
β οΈ Warning! do NOT rely on this method as a source of unique IDs. use it for query boundaries only. that's the whole assignment. periodt.
so here's the lore. an OID is a twelve-byte string. typically, the first four bytes are an integer count of seconds since the Unix epoch, in big-endian format. the remaining bytes are there to keep things unique.
with this method:
- the first argument is an epoch time (in integer seconds)
- the second argument is the remaining eight bytes to append to the string
when you call it as a class method, it returns a brand new BSON::OID object. when you call it as an object method, it mutates the existing internal OID value. same method, two moods. π
and here's the special case: if the second argument is defined and zero ("0"), then the remaining bytes get zeroed. like this:
my $oid = BSON::OID->from_epoch(1467545180, 0);
this is particularly useful when you're looking for documents by their insertion date β you can just look for OIDs that are greater or lower than the one this method generated. chef's kiss. πβ¨
now for the backwards-compat tea β: for compatibility with Mango, if you call it without a second argument, it generates the remainder of the fields "like usual". that's equivalent to calling BSON::OID->new and replacing the first four bytes with the packed epoch value.
# UNSAFE: don't do this unless you have to
my $oid = BSON::OID->from_epoch(1467545180);
like, it literally says # UNSAFE in the original. i'm just the messenger. π
but if you insist on creating a unique OID with from_epoch, then set the remaining eight bytes in a way that guarantees thread-safe uniqueness β like from a reliable source of randomness. the docs point at Crypt::URandom:
use Crypt::Random 'urandom';
my $oid = BSON::OID->from_epoch(1467545180, urandom(8));
there you go. safe, random, gorgeous. πΈ
hex
returns the "oid" attribute as a 24-byte hexadecimal value. short method, big flex. π
get_time
returns a number corresponding to the portion of the "oid" value that represents seconds since the epoch. time travel but make it bytes. β°β¨
TO_JSON
returns a string for this OID, with the OID given as 24 hex digits.
BUT β if the "BSON_EXTJSON" option is true, it will instead be compatible with MongoDB's extended JSON format:
https://github.com/mongodb/specifications/blob/master/source/extended-json.rst
which represents it as a document like this:
{"$oid" : "012345678901234567890123"}
two formats, one method. she contains multitudes. π¦
π OVERLOAD π
the string operator is overloaded, so any string operations will actually use the 24-character hex value of the OID. and fallback overloading is enabled. slay. β¨
both numeric comparison (<=>) and string comparison (cmp) are overloaded to do string comparison of the 24-character hex value of the OID.
β οΈ but listen: if you use them with a non-BSON::OID object, you'd better provide a 24-character hex string, or the results are undefined. no cap, that's a real warning. π
π§ THREADS π§
this module is thread safe. that's it. that's the section. go off, queen. π
π AUTHORS π
- David Golden
<david@mongodb.com> - Stefan G.
<minimalist@lavabit.com>
π COPYRIGHT AND LICENSE π
this software is Copyright (c) 2020 by Stefan G. and MongoDB, Inc.
and it's free software, licensed under:
The Apache License, Version 2.0, January 2004
okay that's the whole page bestie, you're officially an OID expert πβ¨ this was BSON::OID(3pm), perl v5.30.3, dated 2020-08-14 β and yes i read all of it so you didn't have to. mwah. πππ«Ά
π¦ where this page comes from
- debian bookworm: libbson-perl 1.12.2-1 Β· Apache-2.0
- ubuntu jammy: libbson-perl 1.12.2-1 Β· Apache-2.0
- ubuntu noble: libbson-perl 1.12.2-1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms