1BSON_OID(3) libbson BSON_OID(3)
2
3
4
5Libbson provides a simple way to generate ObjectIDs. It can be used in a sin‐
6gle-threaded or multi-threaded manner depending on your requirements.
7
8The bson_oid_t structure represents an ObjectID in MongoDB. It is a 96-bit
9identifier.
10
12 • 4 bytes : The UNIX timestamp in big-endian format.
13
14 • 5 bytes : A random number.
15
16 • 3 bytes : A 24-bit monotonic counter incrementing from rand() in
17 big-endian.
18
20 The typical way to sort in C is using qsort(). Therefore, Libbson pro‐
21 vides a qsort() compatible callback function named bson_oid_compare().
22 It returns less than 1, greater than 1, or 0 depending on the equality
23 of two bson_oid_t structures.
24
26 If you simply want to compare two bson_oid_t structures for equality,
27 use bson_oid_equal().
28
30 To generate a bson_oid_t, you may use the following.
31
32 bson_oid_t oid;
33
34 bson_oid_init (&oid, NULL);
35
37 You can also parse a string containing a bson_oid_t. The input string
38 MUST be 24 characters or more in length.
39
40 bson_oid_t oid;
41
42 bson_oid_init_from_string (&oid, "123456789012345678901234");
43
44 bson_oid_t oid;
45
46 bson_oid_init_from_string_unsafe (&oid, "123456789012345678901234");
47
49 If you need to store items in a hashtable, you may want to use the
50 bson_oid_t as the key. Libbson provides a hash function for just this
51 purpose. It is based on DJB hash.
52
53 unsigned hash;
54
55 hash = bson_oid_hash (oid);
56
58 You can easily fetch the time that a bson_oid_t was generated using
59 bson_oid_get_time_t().
60
61 time_t t;
62
63 t = bson_oid_get_time_t (oid);
64 printf ("The OID was generated at %u\n", (unsigned) t);
65
67 MongoDB, Inc
68
70 2017-present, MongoDB, Inc
71
72
73
74
751.25.1 Nov 08, 2023 BSON_OID(3)