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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mongotransactions-1.2.5.tar.gz.
File metadata
- Download URL: mongotransactions-1.2.5.tar.gz
- Upload date:
- Size: 15.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
65172dee09a95cc24815c7a2c09e7083c9db9fde5f39c107ce7e441b3cda227c
|
|
| MD5 |
fce27b3516760e8d9cf8f3693033e4b1
|
|
| BLAKE2b-256 |
13a496f0676a2431ddadf597f4feef32f3591be476e9dcdc9cf9978d719c6d34
|
File details
Details for the file mongotransactions-1.2.5-py3-none-any.whl.
File metadata
- Download URL: mongotransactions-1.2.5-py3-none-any.whl
- Upload date:
- Size: 15.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7d07c61cb8b497605636420cb3bf8a74edeae90440dbaad8a6c01fcdaef4cfca
|
|
| MD5 |
44e95dcb250faedbbe5b09b4ed10ff08
|
|
| BLAKE2b-256 |
e1a774e57f5759ebd73587da8f191445d8e7cc21694fa76ce4bb443b1da73361
|