class ObjectId

Library: MongoDB
Package: MongoDB
Header: Poco/MongoDB/ObjectId.h


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

  • a 4-byte timestamp,
  • a 3-byte machine identifier,
  • a 2-byte process id, and
  • a 3-byte counter, starting with a random value.

In MongoDB, documents stored in a collection require a unique _id field that acts as a primary key. Because ObjectIds are small, most likely unique, and fast to generate, MongoDB uses ObjectIds as the default value for the _id field if the _id field is not specified; i.e., the mongod adds the _id field and generates a unique ObjectId to assign as its value.

Member Summary

Member Functions: timestamp, toString

Types Aliases


using Ptr = SharedPtr < ObjectId >;



explicit ObjectId(
    const std::string & id

Creates an ObjectId from a string.

The string must contain a hexadecimal representation of an object ID. This means a string of 24 characters.


    const ObjectId & copy

Creates an ObjectId by copying another one.


~ObjectId virtual

virtual ~ObjectId();

Destroys the ObjectId.

Member Functions

timestamp inline

Timestamp timestamp() const;

Returns the timestamp which is stored in the first four bytes of the id


std::string toString(
    const std::string & fmt = "%02x"
) const;

Returns the id in string format. The fmt parameter specifies the formatting used for individual members of the ID char array.