from datetime import datetime
from pymongo import MongoClient
# Replace these with the appropriate values
mongodb_connection_string = "mongodb://your_username:your_password@your_host:your_port/your_database"
collection_name = "your_collection_name"
def insert_current_date(collection, date_iso8601):
document = {"date": date_iso8601}
result = collection.insert_one(document)
print(f"Inserted document with _id: {result.inserted_id}")
if __name__ == "__main__":
# Connect to MongoDB
client = MongoClient(mongodb_connection_string)
db = client.get_database()
collection = db[collection_name]
# Get the current date in ISO 8601 format
current_date = datetime.now().isoformat()
# Insert the current date into MongoDB
insert_current_date(collection, current_date)
With a loop
import time
from datetime import datetime
from pymongo import MongoClient
# Replace these with the appropriate values
mongodb_connection_string = "mongodb://your_username:your_password@your_host:your_port/your_database"
collection_name = "your_collection_name"
def insert_current_date(collection, date_iso8601):
document = {"date": date_iso8601}
result = collection.insert_one(document)
print(f"Inserted document with _id: {result.inserted_id}")
if __name__ == "__main__":
# Connect to MongoDB
client = MongoClient(mongodb_connection_string)
db = client.get_database()
collection = db[collection_name]
while True:
# Get the current date in ISO 8601 format
current_date = datetime.now().isoformat()
# Insert the current date into MongoDB
insert_current_date(collection, current_date)
# Sleep for 1 second
time.sleep(1)