
- 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
How to update a Timestamp and set to current date in MongoDB?
To update, use update() in MongoDB. To set it to current date, you need to get the current date −
var todayDate = new Date();
Let us first create a collection with documents −
> db.demo644.insertOne({"ShippingDate":new ISODate("2018-04-19")}); { "acknowledged" : true, "insertedId" : ObjectId("5e9c76896c954c74be91e6e6") } > db.demo644.insertOne({"ShippingDate":new ISODate("2019-01-10")}); { "acknowledged" : true, "insertedId" : ObjectId("5e9c76966c954c74be91e6e7") }
Display all documents from a collection with the help of find() method −
> db.demo644.find();
This will produce the following output −
{ "_id" : ObjectId("5e9c76896c954c74be91e6e6"), "ShippingDate" : ISODate("2018-04-19T00:00:00Z") } { "_id" : ObjectId("5e9c76966c954c74be91e6e7"), "ShippingDate" : ISODate("2019-01-10T00:00:00Z") }
Following is the query to update timestamp −
> var todayDate = new Date(); > todayDate .setHours(todayDate.getHours() + 1); 1587315898453 > db.demo644.update({}, {$set: {ShippingDate:todayDate}}, {multi: true}); WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
Display all documents from a collection with the help of find() method −
> db.demo644.find();
This will produce the following output −
{ "_id" : ObjectId("5e9c76896c954c74be91e6e6"), "ShippingDate" : ISODate("2020-04-19T17:04:58.453Z") } { "_id" : ObjectId("5e9c76966c954c74be91e6e7"), "ShippingDate" : ISODate("2020-04-19T17:04:58.453Z") }
- Related Articles
- Set current date and time to timestamp in MySQL
- How to convert date to timestamp in MongoDB
- How to update column values with date records and set 1 for corresponding records before the current date in SQL
- Incrementing a date in JavaScript, in order to update MongoDB?
- Display Timestamp before the current date in MySQL
- How to get current timestamp and relative timestamps in PostgreSQL?
- Update record on a specific date matching the current date in MySQL
- How to update a timestamp field of a MySQL table?
- Python Pandas - Get the current date and time from Timestamp object
- How to update of incorrect timestamp format in a DB2 table?
- Creating a Table in MySQL to set current date as default
- How to select date from timestamp in MySQL?
- How to search for a date in MySQL timestamp field?
- How to convert Python date to Unix timestamp?
- In MongoDB how do you use $set to update a nested value/embedded document?

Advertisements