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()
Release files for mongotransactions 1.2.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| mongotransactions-1.2.5.tar.gz | 15.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mongotransactions-1.2.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 31.3 kB
Release files / mongotransactions-1.2.5.tar.gz
| Download URL | mongotransactions-1.2.5.tar.gz |
|---|---|
| Size | 15.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
65172dee09a95cc24815c7a2c09e7083c9db9fde5f39c107ce7e441b3cda227c
|
|
BLAKE2b-256 checksum How to use checksums |
13a496f0676a2431ddadf597f4feef32f3591be476e9dcdc9cf9978d719c6d34
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.4
|
Release files / mongotransactions-1.2.5-py3-none-any.whl
| Download URL | mongotransactions-1.2.5-py3-none-any.whl |
|---|---|
| Size | 15.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7d07c61cb8b497605636420cb3bf8a74edeae90440dbaad8a6c01fcdaef4cfca
|
|
BLAKE2b-256 checksum How to use checksums |
e1a774e57f5759ebd73587da8f191445d8e7cc21694fa76ce4bb443b1da73361
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.4
|