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

1 Like

Hi Aasawari,

Thank you for this post. It appears to be exactly what I was looking for, to logg my weather station data to MongoDB.

I’m a NOOB, trying to setup your script on a Raspberry Pi. Tried running your script with my Atlas connection string, installing (using pip install pymongo and pip install bson) with and without venv and I get the same error:
MongoClient in pymongo not found and BSON not found.
I do run MS Code and when I copied the Python script into Code, it installed MS Python and I fear I could have multiple Python installations and that might be at least one reason.

I’d be gratefiul for any tips.

Kind regards,

Hasse Hellstrom

Latest error.
Apparently, I can choose Python version in Code and I made sure the dependancies are installed and that I run in the venv.

1 Like

Hi @Hasse_Hellstrom!

I am pretty certain this is because you have a version of PyMongo installed alongside an incompatible version of the bson library (that PyMongo uses).

The quickest solution is probably to uninstall both PyMongo & bson, and then re-install PyMongo. You don’t need to re-install the bson library becaause it will be automatically reinstalled at the correct version.

python -m pip uninstall pymongo bson
python -m pip install pymongo

Let me know if this doesn’t fix your problem!

Thanks Mark!
Looks like your suggestion worked.
Haven’t yet tried the provided script, but I managed to write a document to the db, using my test script.

Many thanks for the super quick reply!

//Hasse

Fantastic! Best of luck with your project :slight_smile: