Welcome to mongotriggers’s documentation!¶
This package provides a pythonic interface to allow real time updating, when MongoDB is updated, this update could be up to application layer, or just reside in the backend of the application.
Modern applications are event-driven and not query-driven, therefore whenever there is an update, the application would like to receive a feedback. This package enables this kind of behaviour.
Index¶
Getting Started
Install MongoTriggers¶
You can install dask with pip
, or by installing from source.
Examples¶
Simple¶
This example provides the simplest option for using the package.
from mongotriggers import MongoTrigger
from pymongo import MongoClient
def notify_manager(op_document):
print ('wake up! someone is adding me money')
client = MongoClient(host='localhost', port=27017)
triggers = MongoTrigger(client)
# listens to update/insert/delete, any of these will trigger the callback
triggers.register_op_trigger(notify_manager, 'my_account', 'my_transactions')
triggers.tail_oplog()
# make an operation to simulate interaction
client['my_account']['my_transactions'].insert_one({"balance": 1000})
triggers.stop_tail()
Tail from certain point in time¶
This example provides explanations on how to start listening only from a certain point in time, usually this will be helpful when persistency is required.
from mongotriggers import MongoTrigger
from pymongo import MongoClient
from bson.timestamp import Timestamp
import time
def notify_manager(op_document):
print ('wake up! someone is adding me money')
client = MongoClient(host='localhost', port=27017)
# do something in collection to verify it is not called
client['my_account']['my_transactions'].insert_one({"balance": 1000})
# long waiting time due to timestamp
time.sleep(5)
now = Timestamp(datetime.datetime.utcnow(), 0)
# will get notified only if event occurred after specified now
triggers = MongoTrigger(client, since=now)
triggers.register_op_trigger(notify_manager, 'my_account', 'my_transactions')
triggers.tail_oplog()
# write to collection to verify we receive the callback
client['my_account']['my_transactions'].insert_one({"balance": 1000})
triggers.stop_tail()
API Reference
MongoTriggers¶
-
class
mongotriggers.mongotriggers.
MongoTrigger
(conn, since=None)[source]¶ Bases:
object
-
register_op_trigger
(func, db_name=None, collection_name=None)[source]¶ Watches the specified database and collections for any changes
Parameters:
-
register_insert_trigger
(func, db_name=None, collection_name=None)[source]¶ Adds an insert callback to the specified namespace
Parameters:
-
register_update_trigger
(func, db_name=None, collection_name=None)[source]¶ Adds ann update callback to the specified namespace
Parameters:
-
register_delete_trigger
(func, db_name=None, collection_name=None)[source]¶ Adds a delete callback to the specified namespace
Parameters:
-
unregister_op_trigger
(func, db_name=None, collection_name=None)[source]¶ Removes all callbacks from the specified namespace
Parameters:
-
unregister_insert_trigger
(func, db_name=None, collection_name=None)[source]¶ Removes an insert callback from the specified namespace
Parameters:
-
unregister_update_trigger
(func, db_name=None, collection_name=None)[source]¶ Removes an update callback from the specified namespace
Parameters:
-
Help & Reference
Contact and Support¶
Where to ask for help and provide feedback¶
If you have any question or you just want to help us make the documentation better! Please create an issue in mongotriggers issues in Github Issue Tracker.