
- 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
Converting string to date in MongoDB?
To convert the string to date in MongoDB, use the following syntax:
db.yourCollectionName.aggregate( [ { $project: { anyVariableName: { $dateFromString: { dateString: '$yourFieldName’ } } } } ] );
To understand the above syntax, let us create a collection with some documents. The query to create a collection with documents are as follows:
> db.ConvertStringToDateDemo.insertOne({"ArrivalDate":"20-10-2019"}); { "acknowledged" : true, "insertedId" : ObjectId("5c6ef3596fd07954a489069f") } > db.ConvertStringToDateDemo.insertOne({"ArrivalDate":"21-02-2019"}); { "acknowledged" : true, "insertedId" : ObjectId("5c6ef3616fd07954a48906a0") } > db.ConvertStringToDateDemo.insertOne({"ArrivalDate":"10-12-2018"}); { "acknowledged" : true, "insertedId" : ObjectId("5c6ef36d6fd07954a48906a1") } > db.ConvertStringToDateDemo.insertOne({"ArrivalDate":"31-01-2017"}); { "acknowledged" : true, "insertedId" : ObjectId("5c6ef37b6fd07954a48906a2") }
Display all documents from a collection with the help of find() method. The query is as follows:
> db.ConvertStringToDateDemo.find().pretty();
The following is the output:
{ "_id" : ObjectId("5c6ef3596fd07954a489069f"), "ArrivalDate" : "20-10-2019" } { "_id" : ObjectId("5c6ef3616fd07954a48906a0"), "ArrivalDate" : "21-02-2019" } { "_id" : ObjectId("5c6ef36d6fd07954a48906a1"), "ArrivalDate" : "10-12-2018" } { "_id" : ObjectId("5c6ef37b6fd07954a48906a2"), "ArrivalDate" : "31-01-2017" }
Here is the query to convert string to date:
> db.ConvertStringToDateDemo.aggregate( [ { ... $project: { ... StringToDate: { ... $dateFromString: { ... dateString: '$ArrivalDate' ... } ... } ... } ... } ] ).pretty();
The following is the output:
{ "_id" : ObjectId("5c6ef3596fd07954a489069f"), "StringToDate" : ISODate("2019-10-20T00:00:00Z") } { "_id" : ObjectId("5c6ef3616fd07954a48906a0"), "StringToDate" : ISODate("2019-02-21T00:00:00Z") } { "_id" : ObjectId("5c6ef36d6fd07954a48906a1"), "StringToDate" : ISODate("2018-12-10T00:00:00Z") } { "_id" : ObjectId("5c6ef37b6fd07954a48906a2"), "StringToDate" : ISODate("2017-01-31T00:00:00Z") }
- Related Articles
- Converting a string to a date in JavaScript
- Converting a date in MySQL from string field?
- Converting isodate to numerical value in MongoDB?
- How to convert from string to date data type in MongoDB?
- Converting String to StringBuilder in Java
- Converting ArrayList to String[] in java
- Converting string to a binary string - JavaScript
- Convert date parts to date in MongoDB
- MongoDB – Fix “Failed to convert from type String to type Date”?
- Converting a StringBuilder to String in Java
- Converting string to MORSE code in JavaScript
- Converting string to an array in JavaScript
- Converting list string to dictionary in Python
- Converting whitespace string to url in JavaScript
- Converting multi-dimensional array to string in JavaScript

Advertisements