Local DB vs Atlas: Different field type for dateTime

Hello :grinning:

I am using this python code to insert documents that contain createdDateTime field:

date_format = "%Y-%m-%dT%H:%M:%SZ" 
for item in results['value']:
                item['createdDateTime'] = datetime.strptime(item['createdDateTime'], date_format)
mongo_collection.insert_many(results['value'], ordered=True) 

However, I get different types for the filed in my local DB on Windows and Atlas:

Local:
image

Atlas:
createdDateTime: date
(sorry, not allowed to post two images)

That makes it difficult to develop queries, for example
{ createdDateTime: { $gte: '2023-08-03', $lt: '2023-08-05' } }
works in local but returns 0 results in Atlas.

Any ideas? I am on my first week of MongoDb journey, so apologies for a newbie question :pray:

You are clearly converting native date to string.

So both Atlas and local server should store your createdDateTime in the same string format. It would be a major flaw if they would.

So what ever happens is not related to where the server is running. Most likely the document where createdDateTime is stored in date format have been inserted before the code was changed or are inserted by another piece of code.

By the way it is more efficient to store dates in the date format compared to string. Date takes less space than the string version, date is much faster to compare than string and provides a richer API.

Hey Steeve
Thanks for the reply

It is quite opposite, I am converting a string to datetime object. I use the same code to insert data into my local DB and Atlas. But get different types for the field, string in local but datetime in Atlas.
not sure why.

My bad. I am not into python. Like I wrote it would be a major and know flaw if the same code (including mongodb driver, python and imported library versions) would produce different data type in the local server versus Atlas. May be if your local server is a very very old version where date data type was not existent. But I doubt. Something else is at stake and there is not enough information to help find the issue.

I still suspect that the documents with the wrong datatype have been created before the code that does the conversion of items.

I am not into python but I know it depends on spaces for indentation. May be the invisible space based indentation has been modified between the correct and wrong data type.

I will hand over the baton to any one with python experience because I am 99% certain that the issue is with the input data or the python code. May be you date_format is not correct anymore and datetime.strptime silently do not convert the string to date. May be you can update the code and check what strptime really modified the string to date. You could also check the return code of insert_many to see if documents are really inserted, because you might be simply looking at old documents that were not converted to date.

Hi Stevee,

Thanks for the comprehensive answer.

I agree that it would be a major flow to save the same data in different format. Nevertheless, I’ve recreated the database and it now inserts dateTime as dateTime :yum:
Could be indeed an issue on the data source type as you said, just coincided with the new db creation.

Thanks again for replying.
Dmitry

1 Like