Robert O'Brien
Robert O'Brien

Reputation: 183

Converting date string (YYYY-MM-DD) to datetime object pymongo

Before mass inserting using insertmany(), I need to change the "Date" field of each document from a string which is in the format of 'YYYY-MM-DD' (for example '2020-02-28) to a datetime object which can be used in mongo for later purposes...

Is there a possible way of doing this using pymongo

So my idea would look something like this

dict["Date"] = Mongo_Date(dict["Date"]) #converting the original string to a date object
outputList.append(dict)

#Later on in code
mycol.insert_many(outputList)

is there any easy way of doing this with pymongo??

Upvotes: 0

Views: 481

Answers (1)

Joe
Joe

Reputation: 28366

A couple of possibilities come to mind:

  • use the python map function to modify all of the objects at once
  • insert the objects into MongoDB, and then use update with $dateFromString to modify them

Upvotes: 1

Related Questions