Skip to main content

#db.ObjectId()

Generates a MongoDB ObjectId.

MongoDB ObjectIds are a 12-byte, unique value primarily used for marking unique documents in the db. #db.ObjectId() can be used to generate arbitrary, unique ObjectIds.

Syntax

#db.ObjectId()

Breakdown

ObjectIds convey a variety of information, consisting of:

  • A 4-byte timestamp, measured in seconds since the Unix epoch. This timestamp marks the time the ObjectId was generated
  • A 5-byte value denoting the worker ID the ObjectId was generated on (static across hackmud's workers), as well as the thread ID it was generated on
  • A 3-byte incrementing counter

"65eaa4657b97fd26dd0c5305"

Example

function(context, args) {
return #db.ObjectId() // {"$oid":"65eaa4657b97fd26dd0c5305"}
}