Create a python script that insert document in every minute

document will have three fields
{_id:ObjectID(), t:”S_XXX”, mod_timestamp: “YYYY-MM-DDTHH24:MI:SS”}

where _id - system generated id, t: string with the format S_XXX where XXX - sequential number of minutes in current 2hour pair from 0 to 120 (i.e. hours 00:01 will have 0…120, hours 02-03 - again will have 0…120, 04-05, … and the last pair of hours will be 22:23 and will have again 0…120). i.e we will have always 120 documents at maximum in collection. and application should check if record exists - update mod_timestamp field for this t field, if does not exist - insert new record.

Hi @Kingshuk_Modak and welcome to MongoDB community forums!!

I see your post has been unanswered for a long time. I hope you have been able to resolve the issue.
If not, can you try the code below to achieve the desired output:

from pymongo import MongoClient
from datetime import datetime, timedelta
from bson import ObjectId
import time

client = MongoClient("mongodb+srv://community:community@cluster0.4zdj5vb.mongodb.net/?retryWrites=true&w=majority")
db = client["test"]
collection = db["sample"]

def generate_document():
    current_time = datetime.now()
    sequential_number = (current_time.hour * 60 + current_time.minute) % 120
    document = {
        "_id": ObjectId(),
        "t": f"S_{sequential_number:03d}",
        "mod_timestamp": current_time.strftime("%Y-%m-%dT%H:%M:%S")
    }
    return document

def insert_or_update_document(document):
    query = {"t": document["t"]}
    existing_doc = collection.find_one(query)

    if existing_doc:
        collection.update_one(query, {"$set": {"mod_timestamp": document["mod_timestamp"]}})
    else:
        collection.insert_one(document)

def main():
    while True:
        document = generate_document()
        insert_or_update_document(document)
        print(f"Inserted/Updated document: {document}")
        time.sleep(60)  # Wait for 1 minute

if __name__ == "__main__":
    main()

For which the output has been received as inserted document in every minute.


Inserted/Updated document: {'_id': ObjectId('655b24a4412ae7d3a5ca2445'), 't': 'S_049', 'mod_timestamp': '2023-11-20T14:49:32'}
Inserted/Updated document: {'_id': ObjectId('655b24e3412ae7d3a5ca2446'), 't': 'S_050', 'mod_timestamp': '2023-11-20T14:50:35'}

Also, please note that this has been tested based on the requirements shared and the recommendation would be to perform thorough testing before implementing on the production environment.

Regards
Aasawari