- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to convert date to timestamp in MongoDB
To convert date to timestamp in MongoDB, use aggregate(). Let us create a collection with documents −
> db.demo93.insertOne({"UserName":"Chris","ArrivalDate":new ISODate("2020-10-01")}); { "acknowledged" : true, "insertedId" : ObjectId("5e2d4b6479799acab037af68") } > db.demo93.insertOne({"UserName":"David","ArrivalDate":new ISODate("2019-12-31")}); { "acknowledged" : true, "insertedId" : ObjectId("5e2d4b7379799acab037af69") }
Display all documents from a collection with the help of find() method −
> db.demo93.find();
This will produce the following output −
{ "_id" : ObjectId("5e2d4b6479799acab037af68"), "UserName" : "Chris", "ArrivalDate" : ISODate("2020-10-01T00:00:00Z") } { "_id" : ObjectId("5e2d4b7379799acab037af69"), "UserName" : "David", "ArrivalDate" : ISODate("2019-12-31T00:00:00Z") }
Following is the query to convert date to timestamp in MongoDB −
> db.demo93.aggregate([ ... { "$match": { "UserName": "Chris" }}, ... { "$addFields": { ... "timestamp": { "$toLong": "$ArrivalDate" } ... }} ... ]);
This will produce the following output −
{ "_id" : ObjectId("5e2d4b6479799acab037af68"), "UserName" : "Chris", "ArrivalDate" : ISODate("2020-10-01T00:00:00Z"), "timestamp" : NumberLong("1601510400000") }
- Related Articles
- How to convert Python date to Unix timestamp?
- Convert date string to timestamp in Python
- How to convert unix timestamp string to readable date in Python?
- How to convert a timestamp object in to Date in JDBC program?
- How to convert a Date object in to Timestamp in JDBC program?
- How to update a Timestamp and set to current date in MongoDB?
- Convert date parts to date in MongoDB
- Convert MySQL Unix-Timestamp format to date format?
- Convert DATE timestamp to return the month number
- Convert DATE timestamp to return only the month name in MySQL
- How to convert from string to date data type in MongoDB?
- How to convert birth date records to age with MongoDB
- How to convert from Unix timestamp to MySQL timestamp value?
- Convert MySQL timestamp to UNIX Timestamp?
- How to convert timestamp to datetime in MySQL?

Advertisements