
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
Inserting the current datetime in MongoDB?
To insert current datetime in MongoDB, use the $setOnInsert operator. Let us first implement the following query to create a collection with documents
>db.addCurrentDateTimeDemo.insertOne({"StudentName":"John","StudentAdmissionDate":new Date("2012-01-21") }); { "acknowledged" : true, "insertedId" : ObjectId("5c97ae45330fd0aa0d2fe49f") } >db.addCurrentDateTimeDemo.insertOne({"StudentName":"Carol","StudentAdmissionDate":new Date("2013-05-24") }); { "acknowledged" : true, "insertedId" : ObjectId("5c97ae54330fd0aa0d2fe4a0") } >db.addCurrentDateTimeDemo.insertOne({"StudentName":"Carol","StudentAdmissionDate":new Date("2019-07-26") }); { "acknowledged" : true, "insertedId" : ObjectId("5c97ae5f330fd0aa0d2fe4a1") }
Following is the query to display all documents from a collection with the help of find() method
> db.addCurrentDateTimeDemo.find().pretty();
This will produce the following output
{ "_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") }
Following is the query to insert current date time. We are inserting a new Student record and within that the current date time
> db.addCurrentDateTimeDemo.update( { _id: 1 }, { $set: { StudentName: "Robert" }, $setOnInsert: { StudentAdmissiondate: new Date() } }, { upsert: true } ); WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 1 })
Following is the query to display all documents in order to verify that the current date time is inserted or not
> db.addCurrentDateTimeDemo.find().pretty();
This will produce the following output
{ "_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" }
Look at the above sample output, we have inserted current date time, which is “2019-03-24T16:21:21.269Z”.
- Related Articles
- How to fix the incorrect datetime value while inserting in a MySQL table?
- Inserting Date() in MongoDB through Mongo shell?
- Escaping quotes while inserting records in MongoDB?
- Insert current date in datetime format MySQL?
- MySQL query to get current datetime and only current date
- C# DateTime to add days to the current date
- How to subtract 30 days from the current datetime in MySQL?
- How to subtract 10 days from the current datetime in MySQL?
- Convert the value of the current DateTime object to UTC in C#
- How to restrict inserting an item with the same name in MongoDB?
- State qualitatively the effect of inserting an iron core into a current-carrying solenoid
- MySQL query to set current date in the datetime field for all the column values
- Convert the value of the current DateTime object to a Windows file time in C#
- Query MongoDB for a datetime value less than NOW?
- Find current and previous documents in MongoDB

Advertisements