Issue with date and query

Hi, I have an issue with date and query.

Got a MongoDB with collection and there are records with fields with data:

time_start: 1990-01-02T00:00:00.000+00:00
time_end: 2023-03-06T00:00:00.000+00:00
last_updated: 2023-03-06T21:16:58.096+00:00

So when I wish to find records with ‘find’ I am using:

{'datetime': {'$gte': '2018-01-01', '$lte': '2023-03-09'}}

however it is telling me - TypeError: ‘str’ object is not a mapping

I am not sure if I store properly data - it is ISO 8601 I think, but checked on StackOverflow that such filter with $gte / $lte should work.

Could you please help me? Thank you.

If your dates are stored as date data type you must use data data type in your query.

Rather than

try with

'$gte': new Date('2018-01-01')

The field name in your query must also match a field name from your documents. If your documents have the fields time_start, time_end and last_updated, a query with the field name datetime will not produce any result.

Thanks. Yes I’am using time_start or time_end for query.

Tried to do with new Date(‘’) however it still shows me same error with str.

I have checked the way I add / update records and it is as follows:

"last_updated": datetime.now(pytz.utc)

so it should be ISO 8601 properly added. And even if I find the record and then do to_list(length=None) function it shows as:

'time_end': datetime.datetime(2023, 3, 7, 0, 0)

So a bit confused. Probably something simple, but not see yet. Thanks for help.

It might be helpful if you could share the whole function where you insert and the whole function where you query.

I suspect the error is in the context of query rather than the query itself.

Also specify the programming language. I think it is python by the use of datetime.now() but a confirmation will be nice. You could tag the post with the language.

If you could run an aggregation that uses $type on your date fields to confirm the data type.