No project description provided
Project description
mongotransactions
wrapper around mongoDB transactions and other pymongo operations
basic commands
from mongotransactions import Database
database = Database('mongodb+srv://username:password@host')
# by default the class will use the first database the uri has access to you can change it with
database.set_database('db_name')
# grab a collection
my_collection = database.get_collection('my_collection')
# then you can do your normal operations
for doc in my_collection.find():
print(doc)
first = my_collection.find_one()
# a list of collections is available
print(database.list_collections()) # ['my_collection']
Transactions
package also contains a wrapper around mongo transactions and bulkwrite so that multiple saves/updates/removes across multiple collections to be batch wrote to the server, and it will all roll back if anyone operation fails.
in MongoDB 4.2 and earlier, you cannot create collections in transactions.
The class takes the database class example above
from mongotransactions import Database, Transaction
database = Database('mongodb+srv://username:password@host')
database.set_database('db_name')
transaction = Transaction(database)
# insert operations return a named tuple with id that will be used and the current transactions
insert = transaction.insert('my_collection', { 'name': 'test'})
transaction.insert('events', { 'details': 'new document added', 'doc_id': insert.id})
print(transaction.transactions)
"""
{'my_collection': [InsertOne({'name': 'test', '_id': ObjectId('5f85b1b908820781dc16fe9d')})],
'events': [InsertOne({'details': ' new document added', 'doc_id': ObjectId('5f85b1b908820781dc16fe9d'), '_id': ObjectID('5f85b1b908820781dc16fe9e')})]}
"""
# then finally commit
transaction.run()
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
mongotransactions-1.2.3.tar.gz
(15.5 kB
view hashes)
Built Distribution
Close
Hashes for mongotransactions-1.2.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ed322b3a6779217cf018437e5e7f8048709293b5be3f432d46a87846f0395d07 |
|
MD5 | 7e3ace089a9deb4697fb25d41fccb40b |
|
BLAKE2b-256 | 57bde709af3b9f833440229d82482ed7d8c1a9f7848067a2b977e7dd4dde12ad |