For the complete documentation index, see llms.txt. This page is also available as Markdown.

ObjectId

An ObjectID class is available to scripts in order to facilitate working with BSON object ids used to represent unique identifiers.

ObjectId(string/date/number) Takes a hex string representation of an object id, a date or a timestamp in milliseconds.

Run this script

return {
    date: new ObjectID(new Date()),
    dateDate: new ObjectID(new Date()).toDate(),
    id: new ObjectID(),
    idDate: new ObjectID().toDate(),
    str: new ObjectID("4a232bca18759fcbdef24855"),
    strDate: new ObjectID("4a232bca18759fcbdef24855").toDate(),
    ts: new ObjectID("4a232bca18759fcbdef24855").getTimestamp()
}

And you might get this

{
    "date": "56d615040000000000000000",
    "dateDate": "2016-03-01T22:17:40.000Z",
    "id": "56d615048cacf5d0013e9305",
    "idDate": "2016-03-01T22:17:40.000Z",
    "str": "4a232bca18759fcbdef24855",
    "strDate": "2009-06-01T01:15:54.000Z",
    "ts": 1243818954
}

id.toString() returns the BSON id as a hexstring

id.toJSON() Used primarily by JSON.stringify(), returns the BSON id as a hexstring.

id.equals(other) Returns true if the passed in argument contains the same value as the id. The argument may be a hexstring or an ObjectID instance.

id.toDate() Returns the time the ObjectID was create as a Date value.

id.getTimestamp() Returns the number of seconds between midnight of January 1, 1970 and the time the ObjectID was created.

Last updated

Was this helpful?