Package: pymongo-model
Pymongo ModelWith pymongo-model, it will be easier for you to use pymongo, as you will get a local copy of mongoDB document, you can do any change in the local copy (python object), changes will be committed from local copy to mongoDB only when you invoke save method like it happens in any ORM library.SimpleModel object provides basic feature of updating mongo document locally and commiting at once.Also,
You can use below models for document history tracking feature (that can be used for operations like undo)In DiffHistoryModelV1, we are storing a new document (in delta collection) which consists of version details as well as original document everytime it is updated.In DiffHistoryModelV2, we are usingjson-difffor calculating difference in the versions and storing just the difference object instead of entire document for versioning.InstallationSupports Python 3+
To install, simply use pip$ pip install pymongo-modelUsageimportpymongofrombson.objectidimportObjectIdfrommodel.pymongo_modelimportSimpleModel,DiffHistoryModelV1,DiffHistoryModelV2MONGO_KEY=os.getenv('MONGO_KEY')client=pymongo.MongoClient(MONGO_KEY)db=client["your_database"]Using SimpleModel""" A simple model that wraps mongodb document"""classPlayer(SimpleModel):collection=db.your_collection#mongo collection object#Creating new documentplayer=Player({"username":"pankajsingh.08","name":"Pankaj Singh","age":25,"runs":300})player.save()#commited in db#Updating old documentplayer=Player({"_id":ObjectId("provide_doc_id_here")})#provide mongo document id for fetchingplayer.reload()#this fetches data from db and maps to local dict objectplayer["city"]="Hyderabad"#adding new key to document in local copyplayer["username"]="aa3pankaj"#updating old key in local copydelplayer["age"]#deleting old key in local copyplayer.save()#now everything will be committed at once in dbUsing DiffHistoryModelV1""" A simple model that wraps mongodb document,Also creates a delta_collection for document revision tracking,In this version of DiffHistoryModel, it creates below document in the delta_collection for each update i.e after invoking save method{"collection_name": name of the collection of the document for which revision is being done,"document_id" : mongo id of document for which revision is being done,"document": original document object from your collection for which revision is being done,"_version": revision number of the document,"is_latest":Boolean, true only for latest}"""classMatch(DiffHistoryModelV1):#Refer https://api.mongodb.com/python/current/tutorial.html for creating db objectdb_object=db#mongo client objectcollection=db.your_collection#mongo collection objectdelta_collection_name="_delta_collection"#give any name for delta collection where revisions will be stored, it will be created automatically in the db#Creating new documentmatch=Match({"username":"pankajsingh.08","name":"Pankaj Singh","age":25})match.save()#commited in db#Updating old documentmatch=Match({"_id":ObjectId("provide_doc_id_here")})#provide mongo document id for fetchingmatch.reload()#this fetches data from db and maps to local dict objectmatch["city"]="Pune"#adding new key to document in local copymatch.save()#commited in original collection, Also a new document is created in the delta_collection#helper methodsdoc_latest=match.get_latest_revision()#latest record in dbmatch.delete_latest_revision()#deletes latest record in delta_collection, and makes previous record as latest but original document will not be touchedmatch.undo()#deletes latest record in delta_collection, and makes previous record as latest, Also original document will be updatedUsing DiffHistoryModelV2""" A simple model that wraps mongodb document,Also creates a delta_collection for document revision tracking, Only difference from DiffHistoryModelV1 is here instead of saving entire document, just difference is stored in diff.In this version of DiffHistoryModel, it creates below document in the delta_collection for each update i.e after invoking save method{"collection_name": name of the collection of the document for which revision is being done,"document_id" : mongo id of document for which revision is being done,"diff": diff object generated from json-diff library (https://github.com/fzumstein/jsondiff)"_version": revision number of the document,}"""classMatch(DiffHistoryModelV2):#Refer https://api.mongodb.com/python/current/tutorial.html for creating db objectdb_object=db#mongo client objectcollection=db.your_collection#mongo collection objectdelta_collection_name="_delta_collection"#give any name for delta collection where revisions will be stored, it will be created automatically in the db#Creating new documentmatch=Match({"username":"pankajsingh08","name":"Pankaj Singh","age":"25"})match.save()#commited in db#Updating old documentmatch=Match({"_id":ObjectId("provide_doc_id_here")})#provide mongo document id for fetchingmatch.reload()#this fetches data from db and maps to local dict objectmatch["city"]="Pune"#adding new key to document in local copymatch.save()#commited in original collection, Also a new document is created in the delta_collectionProjects using pymongo-modelPexaBot: A chatbot for cricket match scoring and stats analysisSupportFind me onLinkedin