I am new in mongoDB, please help me for below question description:
I have collection xyz
having one of column insertDate
having date of dd/mm/YYYY
format. I want all records from xyz collection whose insertDate
is greater than 28/01/2022
.
I try below query and it return correct data but return only insertDate column but i want whole json record with above condition:
db.xyz.aggregate([{ $project: { insertDate: { $dateFromString: { format: '%d/%m/%Y', dateString: '$insertDate' } } } }, { $match: { insertDate: { '$gt': new Date("2022-01-01") } } } ]);
Please help me in this problem.
2
Answers
Below query help me:
Where it returns:
Thanks to https://stackoverflow.com/a/72786002/6097074
You’ll need to convert your date in your database from a string to a date, and then filter it. You can do this with the aggregation pipeline.
Let’s start with some data:
Once we’ve inserted these documents we can query it using an aggregation query:
Then we’ll get the following results:
For more information research the $dateFromString function and also the aggregation pipeline