Hello Stanislavas,
In MongoDB queries, you compare data with same types. You don’t compare strings with objects and vice-versa.
new Date(`${year}-01-01`)
`${year}-01-01`
new Date
is of type object (a Date object). The second value is of type string.
For example, consider these two documents in a collection:
{ "_id" : 1, "date" : "2020-05-30" }
{ "_id" : 2, "date" : ISODate("2020-05-30T00:00:00Z") }
And, these two queries:
db.test.find( { date: "2020-05-30" } )
db.test.find( { date: new Date("2020-05-30") } )
The first query returns the document with _id: 1
. And, the second query returns the document with the _id: 2
.