
- 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
MongoDB – Fix “Failed to convert from type String to type Date”?
To fix this, use $dateFromString in MongoDB aggregate(). The $dateFromString converts a date/time string to a date object.
Let us create a collection with documents −
> db.demo619.insertOne({"DueDate":"10-10-2020"}); { "acknowledged" : true, "insertedId" : ObjectId("5e99d7846c954c74be91e69e") } > db.demo619.insertOne({"DueDate":"12-01-2019"}); { "acknowledged" : true, "insertedId" : ObjectId("5e99d7996c954c74be91e69f") } > db.demo619.insertOne({"DueDate":"28-10-2010"}); { "acknowledged" : true, "insertedId" : ObjectId("5e99d7ab6c954c74be91e6a0") }
Display all documents from a collection with the help of find() method −
> db.demo619.find();
This will produce the following output −
{ "_id" : ObjectId("5e99d7846c954c74be91e69e"), "DueDate" : "10-10-2020" } { "_id" : ObjectId("5e99d7996c954c74be91e69f"), "DueDate" : "12-01-2019" } { "_id" : ObjectId("5e99d7ab6c954c74be91e6a0"), "DueDate" : "28-10-2010" }
Following is the query to convert from ty date/time string to a date object −
> db.demo619.aggregate( [ { ... $project: { ... DueDate: { ... $dateFromString: { ... dateString: '$DueDate', ... timezone: 'America/New_York' ... } ... } ... } ... } ] )
This will produce the following output −
{ "_id" : ObjectId("5e99d7846c954c74be91e69e"), "DueDate" : ISODate("2020-10-10T04:00:00Z") } { "_id" : ObjectId("5e99d7996c954c74be91e69f"), "DueDate" : ISODate("2019-01-12T05:00:00Z") } { "_id" : ObjectId("5e99d7ab6c954c74be91e6a0"), "DueDate" : ISODate("2010-10-28T04:00:00Z") }
- Related Articles
- How to convert from string to date data type in MongoDB?
- How to convert string type value to array type in JavaScript?
- How to convert yyyymmdd in INT type to date?
- Convert the number or boolean type JSON object from string type to its original in JavaScript
- Changing data type from date to date/time in MySQL?
- Golang Program to convert double type variables to string
- Golang Program to convert int type variables to String
- Swift Program to convert int type variable to string
- Swift program to convert double type variable to string
- Haskell Program to convert int type variables to String
- Haskell Program to convert double type variables to string
- How to Convert string to float type in Golang?
- How to convert a CLOB type to String in Java?
- C++ Program to Convert String Type Variables into Boolean
- C++ Program to Convert int Type Variables into String

Advertisements