Qore mongodb Module 2.3.0
Loading...
Searching...
No Matches
mongodb::ObjectId Class Reference

The ObjectId class represents a MongoDB ObjectId. More...

#include <QC_ObjectId.dox.h>

Public Member Methods

int compare (ObjectId other)
 Compares this ObjectId to another.
 
 constructor ()
 Creates a new ObjectId with a generated value.
 
 constructor (string hex_string)
 Creates an ObjectId from a 24-character hex string.
 
 copy ()
 Creates a copy of the ObjectId.
 
bool equals (ObjectId other)
 Checks if this ObjectId equals another.
 
date getTimestamp ()
 Returns the timestamp portion of the ObjectId as a date/time value.
 
int hashCode ()
 Returns a hash code for this ObjectId.
 
string toString ()
 Returns the ObjectId as a 24-character hexadecimal string.
 

Detailed Description

The ObjectId class represents a MongoDB ObjectId.

MongoDB ObjectIds are 12-byte identifiers typically used as the default value for the _id field in MongoDB documents. They consist of:

  • 4 bytes representing seconds since Unix epoch
  • 5 bytes of random value
  • 3 bytes of incrementing counter
Example:
# Create a new ObjectId
ObjectId oid();
printf("New ObjectId: %s\n", oid.toString());
# Create from hex string
ObjectId oid2("507f1f77bcf86cd799439011");
# Get timestamp
date dt = oid.getTimestamp();
Since
mongodb 1.0

Member Function Documentation

◆ compare()

int mongodb::ObjectId::compare ( ObjectId  other)

Compares this ObjectId to another.

Code Flags:
CONSTANT
Parameters
otherthe ObjectId to compare to
Returns
-1 if this ObjectId is less than other, 0 if equal, 1 if greater
Example:
int cmp = oid1.compare(oid2);

◆ constructor() [1/2]

mongodb::ObjectId::constructor ( )

Creates a new ObjectId with a generated value.

Example:
ObjectId oid();

◆ constructor() [2/2]

mongodb::ObjectId::constructor ( string  hex_string)

Creates an ObjectId from a 24-character hex string.

Parameters
hex_stringthe 24-character hexadecimal string representation of the ObjectId
Example:
ObjectId oid("507f1f77bcf86cd799439011");
Exceptions
MONGODB-OBJECTID-ERRORthe hex string is not a valid ObjectId

◆ copy()

mongodb::ObjectId::copy ( )

Creates a copy of the ObjectId.

Example:
ObjectId oid2 = oid.copy();

◆ equals()

bool mongodb::ObjectId::equals ( ObjectId  other)

Checks if this ObjectId equals another.

Code Flags:
CONSTANT
Parameters
otherthe ObjectId to compare to
Returns
True if the ObjectIds are equal
Example:
if (oid1.equals(oid2)) {
print("Same ObjectId\n");
}

◆ getTimestamp()

date mongodb::ObjectId::getTimestamp ( )

Returns the timestamp portion of the ObjectId as a date/time value.

Returns
the timestamp when the ObjectId was created
Code Flags:
CONSTANT
Example:
date dt = oid.getTimestamp();

◆ hashCode()

int mongodb::ObjectId::hashCode ( )

Returns a hash code for this ObjectId.

Returns
the hash code
Code Flags:
CONSTANT
Example:
int h = oid.hashCode();

◆ toString()

string mongodb::ObjectId::toString ( )

Returns the ObjectId as a 24-character hexadecimal string.

Returns
the hexadecimal string representation
Code Flags:
CONSTANT
Example:
string str = oid.toString();

The documentation for this class was generated from the following file: