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.
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.
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 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.
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.