Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs Menu
Docs Home
/ / /
C ドライバー
/ /

オブジェクト ID

libbson は、ObjectID を生成する簡単な方法を提供します。 要件に応じて、シングルスレッドまたはマルチスレッドで使用できます。

The bson_oid_t structure represents an ObjectID in MongoDB. It is a 96-bit identifier.

  • 4バイト : ビッグエンディアン形式の UNIX タイムスタンプ。

  • 5バイト : ランダム数。

  • 3バイト : ビッグエンディアンのrand()から増加する24ビットの単調なカウンター。

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

If you simply want to compare two bson_oid_t structures for equality, use bson_oid_equal.

To generate a bson_oid_t, you may use the following.

bson_oid_t oid;
bson_oid_init (&oid, NULL);

You can also parse a string containing a bson_oid_t. The input string MUST be 24 characters or more in length.

bson_oid_t oid;
bson_oid_init_from_string (&oid, "123456789012345678901234");
bson_oid_t oid;
bson_oid_init_from_string_unsafe (&oid, "123456789012345678901234");

If you need to store items in a hashtable, you may want to use the bson_oid_t as the key. Libbson provides a hash function for just this purpose. It is based on DJB hash.

unsigned hash;
hash = bson_oid_hash (oid);

You can easily fetch the time that a bson_oid_t was generated using bson_oid_get_time_t.

time_t t;
t = bson_oid_get_time_t (oid);
printf ("The OID was generated at %u\n", (unsigned) t);

戻る

Handling Errors

項目一覧