BSON_OID(3)
π BSON_OID π β the libbson manual, section 3, bestie
okay so this is the ObjectID page of the libbson manual and honestly she's iconic. Libbson gives you a simple way to generate ObjectIDs, and you can do it in a single-threaded or multi-threaded manner, depending on your requirements. no cap. π
The bson_oid_t structure represents an ObjectID in MongoDB. It is a 96-bit identifier. that's the whole vibe. β¨
𧬠COMPOSITION π§¬
here's what's inside a bson_oid_t, byte by byte, and yes we are counting:
- 4 bytes β the UNIX timestamp, in big-endian format. β°
- 5 bytes β a random number. π²
- 3 bytes β a 24-bit monotonic counter incrementing from
rand(), in big-endian. π
π€ SORTING OBJECTIDS π€
The typical way to sort in C is using qsort(). So Libbson said "say less" and provides a qsort() compatible callback function named bson_oid_compare(). It returns less than 1, greater than 1, or 0, depending on the equality of two bson_oid_t structures. π
πͺ COMPARING OBJECT IDS πͺ
If you simply want to compare two bson_oid_t structures for equality, use bson_oid_equal(). easy peasy, that's the whole thing. π«Ά
π GENERATING π
To generate a bson_oid_t, you may use the following:
bson_oid_t oid;
bson_oid_init (&oid, NULL);
boom, born, slay. πΌβ¨
π PARSING OBJECTID STRINGS π
You can also parse a string containing a bson_oid_t.
β οΈ The input string MUST be 24 characters or more in length. like, non-negotiable, bestie.
bson_oid_t oid;
bson_oid_init_from_string (&oid, "123456789012345678901234");
bson_oid_t oid;
bson_oid_init_from_string_unsafe (&oid, "123456789012345678901234");
#οΈβ£ HASHING OBJECTIDS #οΈβ£
If you need to store items in a hashtable, you may want to use the bson_oid_t as the key. Libbson provides a hash function for just this purpose. It is based on DJB hash. π
unsigned hash;
hash = bson_oid_hash (oid);
β³ FETCHING OBJECTID CREATION TIME β³
You can easily fetch the time that a bson_oid_t was generated using bson_oid_get_time_t(). π°οΈ
time_t t;
t = bson_oid_get_time_t (oid);
printf ("The OID was generated at %u\n", (unsigned) t);
π AUTHOR π
MongoDB, Inc π
π COPYRIGHT π
2017-present, MongoDB, Inc
and that's the page, bestie π this whole thing is libbson 1.26.0, dated Mar 31, 2024, and it lives at BSON_OID(3). go forth and generate your little 96-bit icons. πβ¨π¦
π¦ where this page comes from
- ubuntu noble: libbson-doc 1.26.0-1.1ubuntu2 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms