SysTutorials

Linux Manual Pages › Section 3

bson_oid_t(3)

NAME

bson_oid_t – BSON ObjectID Abstraction

SYNOPSIS


#include <bson.h>
typedef struct
{
    uint8_t bytes[12];
} bson_oid_t;

DESCRIPTION

The bson_oid_t structure contains the 12-byte ObjectId notation defined by the BSON ObjectID specificiation \&.

ObjectId is a 12-byte BSON type, constructed using:

\[bu]

a 4-byte value representing the seconds since the Unix epoch (in Big Endian)

\[bu]

a 3-byte machine identifier

\[bu]

a 2-byte process id (Big Endian), and

\[bu]

a 3-byte counter (Big Endian), starting with a random value.

STRING CONVERSION

You can convert an Object ID to a string using bson_oid_to_string(3) and back with bson_oid_init_from_string(3) \&.

HASHING

A bson_oid_t can be used in hashtables using the function bson_oid_hash(3) and bson_oid_equal(3) \&.

COMPARING

A bson_oid_t can be compared to another using bson_oid_compare(3) for qsort(3) style comparing and bson_oid_equal(3) for direct equality.

VALIDATING

You can validate that a string containing a hex-encoded ObjectID is valid using the function bson_oid_is_valid(3) \&.

EXAMPLE


#include <bson.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
    bson_oid_t oid;
    char str[25];
    bson_oid_init(&oid, NULL);
    bson_oid_to_string(&oid, str);
    printf("%s
", str);
    if (bson_oid_is_valid(str, sizeof str)) {
        bson_oid_init_from_string(&oid, str);
    }
    printf("The UNIX time was: %u
",
           (unsigned)bson_oid_get_time_t(&oid));
    return 0;
}

COLOPHON

This page is part of libbson. Please report any bugs at https://jira.mongodb.org/browse/CDRIVER.

Index