About date filter in Mongodb query

Hi guys
So I have a data with a field “createdDate” type Date. I want to filter out documents from my DB based on documents creation Date. My exact target is to filter documents from a specific date to 2 years before the current date. How could I do this, please someone help.
(Note :- I need query for mongoshell version 3.4.24, I have created working query for higher versions already)

I do not see at first what would be the difference. Share the query you have and explain how it fails to provide the desired output.

db.getCollection(<collectionName>).deleteMany({ "createdDate": {
$gt: ISODate("2020-12-31"),
$lte: new Date(ISODate().getTime() - 1000*86400*365*2)

Read it as Mongodb server version 3.4.24
(Not sure if shell or server need to be the same version or not)

The query seems to be syntactically correct, but logically it is another story.

Two years before today is 2020-12-10, as such no dates can be both $gt:2020-12-31 and $lte:2020-12-10.

Check your math and

Actually, that will be in use from 2023 thats why written that way
You can change the dates and check. It is giving errors, I don’t know why
Some people said it could be a version issue, maybe the line written for $lte is not valid in 3.4.24

I do not have 3.4. What is the error you get? Cut-n-paste the whole error message and/or stack trace.

Failed to retrieve documents

Database error!
|/ java.lang.Exception: [<Database_Name>.<collection_name>[replica set] [direct]] Database error!
/ Mongo Server error (MongoQueryException): Query failed with error code 2 with name ‘BadValue’ and error message 'error processing query: ns=<Database_Name>.<collection_name> batchSize=50Tree: $and
… createdDate $lte new Date(1668206779750)
… createdDate $gt new Date(1656633600000)
… Sort: {}
… Proj: {}
_… No query solutions’ on server <host>.<port>

One more point to be noted is:- when I cloned that collection locally and ran the query on it, it works but not on the actual remote one which is 3.4.24
Makes me think it is a version issue only

The error message really contains the strings <Database_Name>, <collection_name> or you hiding the real name. I find it strange that the error message is not more detailed like it could show the real Database_Name and collection_name rather than place holder.

This seems to indicate that the query you run in real like has more to it than the find you share. May the error lies in the part you did not share.

Yes, I hide the actual database name and collection name because it is elated to my organization. Hope you dont mind
Rest I have shown the complete error

Same goes for and
BTW just the diff between the query I shared with you and the one I used to get this error is
I used diff dates and used find instead of deleteMany because I didn’t wanted to delete the data myself accidentally, just wanted to test the query

BTW I am opening the database and executing the query on Studio3T