Inserting the current datetime in MongoDB?

To insert current datetime in MongoDB, use new Date() during insertion or $setOnInsert with upsert operations. MongoDB stores dates in ISODate format with UTC timezone.

Syntax

// Direct insertion
db.collection.insertOne({ fieldName: new Date() });

// With upsert operation
db.collection.update(
    { _id: value },
    { $set: { field1: "value" }, $setOnInsert: { dateField: new Date() } },
    { upsert: true }
);

Sample Data

db.addCurrentDateTimeDemo.insertMany([
    { "StudentName": "John", "StudentAdmissionDate": new Date("2012-01-21") },
    { "StudentName": "Carol", "StudentAdmissionDate": new Date("2013-05-24") },
    { "StudentName": "Carol", "StudentAdmissionDate": new Date("2019-07-26") }
]);
{
    "acknowledged": true,
    "insertedIds": [
        ObjectId("5c97ae45330fd0aa0d2fe49f"),
        ObjectId("5c97ae54330fd0aa0d2fe4a0"),
        ObjectId("5c97ae5f330fd0aa0d2fe4a1")
    ]
}

View Sample Data

db.addCurrentDateTimeDemo.find().pretty();
{
    "_id": ObjectId("5c97ae45330fd0aa0d2fe49f"),
    "StudentName": "John",
    "StudentAdmissionDate": ISODate("2012-01-21T00:00:00Z")
}
{
    "_id": ObjectId("5c97ae54330fd0aa0d2fe4a0"),
    "StudentName": "Carol",
    "StudentAdmissionDate": ISODate("2013-05-24T00:00:00Z")
}
{
    "_id": ObjectId("5c97ae5f330fd0aa0d2fe4a1"),
    "StudentName": "Carol",
    "StudentAdmissionDate": ISODate("2019-07-26T00:00:00Z")
}

Method 1: Using $setOnInsert with Upsert

Insert a new student record with current datetime using $setOnInsert ?

db.addCurrentDateTimeDemo.update(
    { _id: 1 },
    { 
        $set: { StudentName: "Robert" }, 
        $setOnInsert: { StudentAdmissiondate: new Date() } 
    },
    { upsert: true }
);
WriteResult({ "nMatched": 0, "nUpserted": 1, "nModified": 0, "_id": 1 })

Verify Result

db.addCurrentDateTimeDemo.find().pretty();
{
    "_id": ObjectId("5c97ae45330fd0aa0d2fe49f"),
    "StudentName": "John",
    "StudentAdmissionDate": ISODate("2012-01-21T00:00:00Z")
}
{
    "_id": ObjectId("5c97ae54330fd0aa0d2fe4a0"),
    "StudentName": "Carol",
    "StudentAdmissionDate": ISODate("2013-05-24T00:00:00Z")
}
{
    "_id": ObjectId("5c97ae5f330fd0aa0d2fe4a1"),
    "StudentName": "Carol",
    "StudentAdmissionDate": ISODate("2019-07-26T00:00:00Z")
}
{
    "_id": 1,
    "StudentAdmissiondate": ISODate("2019-03-24T16:21:21.269Z"),
    "StudentName": "Robert"
}

Method 2: Direct Insertion with new Date()

db.addCurrentDateTimeDemo.insertOne({
    "StudentName": "Alice",
    "StudentAdmissionDate": new Date()
});

Key Points

  • new Date() creates a timestamp at the moment of execution
  • $setOnInsert only sets the field if the document is newly created (upserted)
  • MongoDB stores dates in UTC timezone as ISODate objects

Conclusion

Use new Date() for direct current datetime insertion or $setOnInsert with upsert operations to conditionally add timestamps. MongoDB automatically converts JavaScript Date objects to ISODate format.

Updated on: 2026-03-15T00:24:12+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements