Skip to main content

Simple model based usage of pymongo, also provides optional feature for tracking mongoDB document history

Project description

Pymongo Model

With 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)

  1. 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.
  2. In DiffHistoryModelV2, we are using json-diff for calculating difference in the versions and storing just the difference object instead of entire document for versioning.

Installation

Supports Python 3+ To install, simply use pip

$ pip install pymongo-model

Usage

import pymongo
from bson.objectid import ObjectId
from model.pymongo_model import SimpleModel, DiffHistoryModelV1, DiffHistoryModelV2

MONGO_KEY = os.getenv('MONGO_KEY')
client = pymongo.MongoClient(MONGO_KEY)
db = client["your_database"]

Using SimpleModel

""" A simple model that wraps mongodb document 
"""
class Player(SimpleModel):
    collection = db.your_collection   #mongo collection object


#Creating new document
player = Player({"username":"pankajsingh.08","name": "Pankaj Singh", "age": 25,"runs":300})    
player.save()  #commited in db

#Updating old document
player = Player({"_id":ObjectId("provide_doc_id_here")})    #provide mongo document id for fetching
player.reload()     #this fetches data from db and maps to local dict object   
player["city"] = "Hyderabad"  #adding new key to document in local copy
player["username"] = "aa3pankaj" #updating old key in local copy
del player["age"]  #deleting old key in local copy
player.save()  #now everything will be committed at once in db

Using 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
    }
"""
class Match(DiffHistoryModelV1):
    #Refer https://api.mongodb.com/python/current/tutorial.html for creating db object
    db_object = db                                #mongo client object
    collection = db.your_collection               #mongo collection object
    delta_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 document
match = Match({"username":"pankajsingh.08","name": "Pankaj Singh", "age": 25})    
match.save()  #commited in db

#Updating old document
match = Match({"_id":ObjectId("provide_doc_id_here")})    #provide mongo document id for fetching
match.reload()     #this fetches data from db and maps to local dict object   
match["city"] = "Pune"  #adding new key to document in local copy
match.save()  #commited in original collection, Also a new document is created in the delta_collection

#helper methods
doc_latest = match.get_latest_revision()  #latest record in db
match.delete_latest_revision()  #deletes latest record in delta_collection, and makes previous record as latest but original document will not be touched
match.undo()   #deletes latest record in delta_collection, and makes previous record as latest, Also original document will be updated

Using 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,
    }
"""

class Match(DiffHistoryModelV2):
    #Refer https://api.mongodb.com/python/current/tutorial.html for creating db object
    db_object = db                                #mongo client object
    collection = db.your_collection               #mongo collection object
    delta_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 document
match = Match({"username":"pankajsingh08","name": "Pankaj Singh", "age": "25"})    
match.save()  #commited in db

#Updating old document
match = Match({"_id":ObjectId("provide_doc_id_here")})    #provide mongo document id for fetching
match.reload()     #this fetches data from db and maps to local dict object   
match["city"] = "Pune"  #adding new key to document in local copy
match.save()  #commited in original collection, Also a new document is created in the delta_collection

Projects using pymongo-model

  1. PexaBot : A chatbot for cricket match scoring and stats analysis

Support

Find me on Linkedin

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

pymongo_model-2.0.2-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file pymongo_model-2.0.2-py3-none-any.whl.

File metadata

  • Download URL: pymongo_model-2.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.0

File hashes

Hashes for pymongo_model-2.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f344cd2a17942b1c8677056bbab8f661559c83ac57d088f6e35e7f370f1fcfc5
MD5 2ddf0863da94ad96161c6a9086316aa5
BLAKE2b-256 442cbda3b4981890f989badadd9c5d6e8bb8aa5fc1d2a9210100904461988af0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page