Skip to main content

Inserting a date in MongoDB

NOTE: the date is in Zulu time (i.e. UTC)

node -e 'console.log(new Date("2025-02-28T01:23:45.678Z").toLocaleString())'

outputs in my locale:

28/02/2025, 12:23:45 pm

Using Compass:

{
"_id": {
"$oid": "65663525fd6a4cf561748765"
},
"created": {
"$date": "2025-02-28T01:23:45.678Z"
},
"detail": "details appear here"
}

Using the CLI, mongosh:

// insert the current timestamp:
db.example.insertOne({ example: '1', 'date': new Date(Date.now()) });
// these 2 following are equivalent:
db.example.insertOne({ example: '2', 'date': new Date("2025-02-28T01:23:45.678Z") });
db.example.insertOne({ example: '3', 'date': new Date(2025,1,28,12,23,45,678) });