---
name: "bson_oid_t"
section: "3"
platform: "linux"
package: "libbson-doc"
version: "1.30.4-1+deb13u3"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/bson_oid_t"
---

# bson\_oid\_t(3)

BSON ObjectID Abstraction

## SYNOPSIS

```
#include <bson/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 specification*.

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

- •

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

- •

  a 5-byte random value.

- •

  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()* and back with *bson\_oid\_init\_from\_string()*.

## HASHING

A *bson\_oid\_t* can be used in hashtables using the function *bson\_oid\_hash()* and *bson\_oid\_equal()*.

## COMPARING

A *bson\_oid\_t* can be compared to another using *bson\_oid\_compare()* for **qsort()** style comparing and *bson\_oid\_equal()* for direct equality.

## VALIDATING

You can validate that a string containing a hex-encoded ObjectID is valid using the function *bson\_oid\_is\_valid()*.

## EXAMPLE

```
#include <bson/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\n", str);
   if (bson_oid_is_valid (str, sizeof str)) {
      bson_oid_init_from_string (&oid, str);
   }
   printf ("The UNIX time was: %u\n", (unsigned) bson_oid_get_time_t (&oid));
   return 0;
}
```

## AUTHOR

MongoDB, Inc

## COPYRIGHT

2009-present, MongoDB, Inc.
