
- 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
Format date value in MongoDB shell?
To format date value, use $dateToString in MongoDB. Let us create a collection with documents −
> db.demo480.insertOne({id:1,"DueDate":new ISODate("2020-01-10")});{ "acknowledged" : true, "insertedId" : ObjectId("5e821056b0f3fa88e2279098") } > db.demo480.insertOne({id:1,"DueDate":new ISODate("2017-12-21")});{ "acknowledged" : true, "insertedId" : ObjectId("5e821062b0f3fa88e2279099") } > db.demo480.insertOne({id:1,"DueDate":new ISODate("2019-10-12")});{ "acknowledged" : true, "insertedId" : ObjectId("5e82106ab0f3fa88e227909a") } > db.demo480.insertOne({id:1,"DueDate":new ISODate("2019-12-01")});{ "acknowledged" : true, "insertedId" : ObjectId("5e821078b0f3fa88e227909b") }
Display all documents from a collection with the help of find() method −
> db.demo480.find();
This will produce the following output −
{ "_id" : ObjectId("5e821056b0f3fa88e2279098"), "id" : 1, "DueDate" : ISODate("2020-01- 10T00:00:00Z") } { "_id" : ObjectId("5e821062b0f3fa88e2279099"), "id" : 1, "DueDate" : ISODate("2017-12- 21T00:00:00Z") } { "_id" : ObjectId("5e82106ab0f3fa88e227909a"), "id" : 1, "DueDate" : ISODate("2019-10- 12T00:00:00Z") } { "_id" : ObjectId("5e821078b0f3fa88e227909b"), "id" : 1, "DueDate" : ISODate("2019-12- 01T00:00:00Z") }
Following is the query to format date value in MongoDB shell −
> db.demo480.aggregate([ ... { "$match": { "id" : 1 } }, ... { "$sort": { "DueDate": -1 } }, ... { ... "$project": { ... "_id": 0, ... "DueDate": { ... "$dateToString": { ... "format": "%Y-%m-%d %H-%M", ... "date": "$DueDate" ... } ... } ... } ... } ... ])
This will produce the following output −
{ "DueDate" : "2020-01-10 00-00" } { "DueDate" : "2019-12-01 00-00" } { "DueDate" : "2019-10-12 00-00" } { "DueDate" : "2017-12-21 00-00" }
- Related Articles
- Change date format in MongoDB
- Inserting Date() in MongoDB through Mongo shell?
- Extract Numeric Date Value from Date Format in MySQL?
- How to print document value in MongoDB shell?
- How to format date value in JavaScript?
- MongoDB query to fetch date records (ISODate format) in a range
- How to filter a query on specific date format with MongoDB?
- Prettyprint in MongoDB shell as default?
- Java Program to parse string date value with default format
- Unix date format in Java
- Use result from MongoDB in shell script?
- How to get items with a specific value from documents using MongoDB shell?
- MySQL ORDER BY Date field not in date format?
- Get output of MongoDB shell script?
- Format date with DateFormat.MEDIUM in Java

Advertisements