将日期字符串 (YYYY-MM-DD) 转换为日期时间对象 pymongo

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

在使用 insertmany() 进行大量插入之前,我需要将每个文档的 "Date" 字段从格式为 'YYYY-MM-DD' 的字符串(例如 '2020-02-28 ) 到可以在 mongo 中用于以后目的的日期时间对象...

有没有办法使用 pymongo

所以我的想法是这样的

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)

有什么简单的方法可以用 pymongo 做到这一点吗??

我想到了几种可能性:

  • 使用python map 函数一次修改所有对象
  • 将对象插入 MongoDB,然后使用 $dateFromString 更新来修改它们