Search…
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
1
return {
2
date: new ObjectID(new Date()),
3
dateDate: new ObjectID(new Date()).toDate(),
4
id: new ObjectID(),
5
idDate: new ObjectID().toDate(),
6
str: new ObjectID("4a232bca18759fcbdef24855"),
7
strDate: new ObjectID("4a232bca18759fcbdef24855").toDate(),
8
ts: new ObjectID("4a232bca18759fcbdef24855").getTimestamp()
9
}
Copied!
And you might get this
1
{
2
"date": "56d615040000000000000000",
3
"dateDate": "2016-03-01T22:17:40.000Z",
4
"id": "56d615048cacf5d0013e9305",
5
"idDate": "2016-03-01T22:17:40.000Z",
6
"str": "4a232bca18759fcbdef24855",
7
"strDate": "2009-06-01T01:15:54.000Z",
8
"ts": 1243818954
9
}
Copied!
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 modified 1mo ago
Copy link