
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert date parts to date in MongoDB
Let us first create a collection with documents −
> db.demo386.insert( ... { ... details: { Month: 02, Day: 27, Year: 2020 } ... } ... ); WriteResult({ "nInserted" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo386.find();
This will produce the following output −
{ "_id" : ObjectId("5e5bd9a222064be7ab44e7f7"), "details" : { "Month" : 2, "Day" : 27, "Year" : 2020 } }
Following is the query to convert date parts to date −
> db.demo386.aggregate( ... {"$project":{ ... "_id":0, ... "DueDate":{ ... "$dateToString":{ ... "format":"%m-%d-%Y", ... "date":{"$dateFromParts": {"year":"$details.Year","month":"$details.Month","day":"$details.Day"}} ... } ... } ... }} ... );
This will produce the following output −
{ "DueDate" : "02-27-2020" }
- Related Questions & Answers
- How to convert date to timestamp in MongoDB
- Convert UK DATE to MySQL date?
- GroupBy Date in MongoDB to count duplicate date records
- How to convert epoch Date to meaningful JavaScript date?
- How to convert a MySQL date to JavaScript date?
- How to convert birth date records to age with MongoDB
- Get Date and Time parts separately in Java
- How to convert from string to date data type in MongoDB?
- Convert varchar to date in MySQL?
- Convert String to Date in Java
- Converting string to date in MongoDB?
- How to insert Date in MongoDB?
- How to convert a Python date string to a date object?
- How to convert a JS Date to a Python date object?
- How to convert a date or date vector to POSIXct in R?
Advertisements