2023-01-07

Pymongo - Insert current timestamp as ISODate

from pymongo import MongoClient

import datetime



client = MongoClient("mongodb://YOUR_CONNECTION_STRING")

db = client.YOUR_DATABASE_NAME

collection = db.YOUR_COLLECTION_NAME


# Truncate

collection.delete_many({})


# Date treatment

date = datetime.datetime.utcnow()

at = datetime.datetime.strptime(

    str(date),

    "%Y-%m-%d %H:%M:%S.%f"

)


# Insertion

collection.insert_one({"YOUR_FIELD_NAME": at})


No comments:

Post a Comment