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