Date field returns error with query operations

Hi everyone,
I have a problem with a date field. I’m trying to make query him for example trying to find dates older than x date, but I always get an error or nothing is returned. Maybe is the way the field is formatted but I couldn’t find why. If somebody please can help me understand where’s the error.

The date field schema is the following:

"created": {
        "$date": {
            "$numberLong": "1640630359000"
        }
    }

I have like a thousand documents with this field. In mongosh I query a find:

db.clients.find({created: {$lt: new Date('2022-01-08')} })

but the query didn’t returned anything. I also tried to convert to string and had an error and also tried to make a match aggregation and returned nothing. In the aggregation app of compass I can see the field with this kind of value field display:

created: 2021-12-27T18:39:19.000+00:00

and in the document schema in compass it shows is a date field.

Thanks for the help.

What is the error you are getting - please share it here.

Also, check what the data type of the field created is, by running this query in mongo shell:

db.collection.aggregate([
  { $addFields: { created_data_type: { $type: "$created" } } }
])

Then, take a look at the $toDate aggregation operator - this can be used to convert other data types with date information to date type.

Thanks for your help Prasad the error that the aggregation returns me is generic, only that an error happened and with a find() it returns nothing. I ran the query you gave me and the result is that the data type is an object:

created_data_type: 'object'

I will check the $toDate operator to see if it works.

In the find query you are comparing created field with a date new Date('2022-01-08') - this will not work. When you want to compare two values, both the values must be of the same data type.