Search code examples
python-3.xmongodbdatetimeisodate

Why does mongodb change my date formats to long number and how do I fix it?


I have been storing some dummy data on a project I've been working on. Some data include dates in the format of date. But somehow, I saw it changed the format to long number and I have no idea why. Can someone please tell me how to convert the format back to dates? I saw someone suggesting using aggregate $todate. EG:

db.getCollection('my_list').aggregate([
  {
    $project: {
      toDate: {
        $toDate: "$published_date"
      }
    }
  }
])

I have tried that code above but it was not a permanent change. The data result I got as a datetime format in python as: 9/11/2019 12:00 PM.... Code as below:

timestamp = '2019-11-09T12:00:12.000Z'
format = "%Y-%m-%dT%H:%M:%S.%fZ"

some_datetime_obj = datetime.datetime.strptime(timestamp, format)

Some_datetime_obj returns a datetime format.

But when it enters mongodb, I get a NumberLong object. So how do I make a permanent change to all the data in the database?


Solution

  • I figured it out. I don't know why, but how it happened was because I converted my dataframe into a json format then only inserted into mongodb.

    OLD CODE:

    records = json.loads(df.T.to_json()).values()
    vid_client_db[VID_COL].insert_many(records)
    

    NEW CODE:

    records = df.to_dict(orient='row')
    vid_client_db[VID_COL].insert_many(records)