
- 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
How to filter a query on specific date format with MongoDB?
To filter a query on specific date format, use $dateToString. Let us create a collection with documents −
> db.demo433.insertOne({"DueDate":new Date("2019-11-23")}); { "acknowledged" : true, "insertedId" : ObjectId("5e771278bbc41e36cc3cae91") } > db.demo433.insertOne({"DueDate":new Date("2020-01-03")}); { "acknowledged" : true, "insertedId" : ObjectId("5e771290bbc41e36cc3cae92") }
Display all documents from a collection with the help of find() method −
> db.demo433.find();
This will produce the following output −
{ "_id" : ObjectId("5e771278bbc41e36cc3cae91"), "DueDate" : ISODate("2019-11-23T00:00:00Z") } { "_id" : ObjectId("5e771290bbc41e36cc3cae92"), "DueDate" : ISODate("2020-01-03T00:00:00Z") }
Following is the query to filter a query on specific date format in MongoDB −
> db.demo433.aggregate([ ... { $addFields: {stringDate: { $dateToString: { format: "%Y-%m-%d", date: "$DueDate" } } } }, ... { $match: {"stringDate":"2020-01-03"}}, ... { $project:{"stringDate":0}} ... ])
This will produce the following output −
{ "_id" : ObjectId("5e771290bbc41e36cc3cae92"), "DueDate" : ISODate("2020-01-03T00:00:00Z") }
- Related Questions & Answers
- Filter query on array of embedded document with MongoDB?
- UNIX_TIMESTAMP with date in MySQL query to fetch records after a specific date in different format?
- How to find specific array elements in MongoDB document with query and filter with range?
- MongoDB query to fetch date records (ISODate format) in a range
- MongoDB query to get specific month|year (not date)?
- Return query based on date in MongoDB?
- Filter specific values from a MongoDB document
- Set a specific Date Format in MySQL?
- How to work Date query with ISODate in MongoDB?
- Find records on or after a specific date in MongoDB?
- MongoDB query to remove a specific document
- How to convert US date format to MySQL format in INSERT query?
- Change date format in MongoDB
- Filter query by current date in MySQL
- How to filter a specific month in MySQL when date is in varchar?
Advertisements