1BSON_OID(3)                         Libbson                        BSON_OID(3)
2
3
4

NAME

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 that includes various information  about  the  system
14       generating the OID.
15

COMPOSITION

17       · 4 bytes : The UNIX timestamp in big-endian format.
18
19       · 3 bytes : A hash of the hostname.
20
21       · 2 bytes : The pid_t of the current process. Alternatively the task-id
22         if configured.
23
24       · 3 bytes : A 24-bit monotonic  counter  incrementing  from  rand()  in
25         big-endian.
26

SORTING OBJECTIDS

28       The  typical way to sort in C is using qsort(). Therefore, Libbson pro‐
29       vides a qsort() compatible callback function named  bson_oid_compare().
30       It  returns less than 1, greater than 1, or 0 depending on the equality
31       of two bson_oid_t structures.
32

COMPARING OBJECT IDS

34       If you simply want to compare two bson_oid_t structures  for  equality,
35       use bson_oid_equal().
36

GENERATING

38       To generate a bson_oid_t, you may use the following.
39
40          bson_oid_t oid;
41
42          bson_oid_init (&oid, NULL);
43

PARSING OBJECTID STRINGS

45       You  can  also parse a string containing a bson_oid_t. The input string
46       MUST be 24 characters or more in length.
47
48          bson_oid_t oid;
49
50          bson_oid_init_from_string (&oid, "123456789012345678901234");
51
52       If you need to parse may bson_oid_t in a tight loop and  can  guarantee
53       the  data is safe, you might consider using the inline variant. It will
54       be inlined into your code and reduce the need for  a  foreign  function
55       call.
56
57          bson_oid_t oid;
58
59          bson_oid_init_from_string_unsafe (&oid, "123456789012345678901234");
60

HASHING OBJECTIDS

62       If  you  need  to  store  items in a hashtable, you may want to use the
63       bson_oid_t as the key. Libbson provides a hash function for  just  this
64       purpose. It is based on DJB hash.
65
66          unsigned hash;
67
68          hash = bson_oid_hash (oid);
69

FETCHING OBJECTID CREATION TIME

71       You  can  easily  fetch  the time that a bson_oid_t was generated using
72       bson_oid_get_time_t().
73
74          time_t t;
75
76          t = bson_oid_get_time_t (oid);
77          printf ("The OID was generated at %u\n", (unsigned) t);
78

AUTHOR

80       MongoDB, Inc
81
83       2017-present, MongoDB, Inc
84
85
86
87
881.13.1                           Jan 24, 2019                      BSON_OID(3)
Impressum