Object-oriented interactions with MongoDB
Project description
MongoDriver
An object-oriented package for interacting with MongoDB documents
Features
- Object-oriented
- Update documents without needing to use json
- Use pymongo within the package as well
- Built with type hints
Quickstart
Install MongoDriver
python3 -m pip install mongodriver
from mongodriver.mongodriver import Driver
driver = Driver(
connection_url="mongodb+srv://example:SecurePassword@testcluster.e2lhq.mongodb.net/myFirstDatabase?retryWrites=true&w=majority",
db_name="example_db", collection_name="example_collection")
Examples
Here is a basic example on how to create a new document and then interact it
from mongodriver.mongodriver import Driver
driver = Driver(
connection_url="mongodb+srv://example:SecurePassword@testcluster.e2lhq.mongodb.net/myFirstDatabase?retryWrites=true&w=majority",
db_name="example_db", collection_name="example_collection")
new_document = driver.create({"foo": 1, "bar": 2})
# print the value of "foo"
print(new_document.foo) # 1
# change the value of "foo"
new_document.foo = 2
print(new_document.foo) # 2
# you can also change the value of an attribute with the Driver.VarClass.update() method
new_document.foo.update(3)
print(new_document.foo) # 3
Find a document
from mongodriver.mongodriver import Driver
driver = Driver(
connection_url="mongodb+srv://example:SecurePassword@testcluster.e2lhq.mongodb.net/myFirstDatabase?retryWrites=true&w=majority",
db_name="example_db", collection_name="example_collection")
search_query = {"foo": 1}
documents = driver.find(search_query) # returns a list of documents
for document in documents:
print(document)
Load all documents from MongoDB into Document objects
from mongodriver.mongodriver import Driver
driver = Driver(
connection_url="mongodb+srv://example:SecurePassword@testcluster.e2lhq.mongodb.net/myFirstDatabase?retryWrites=true&w=majority",
db_name="example_db", collection_name="example_collection")
documents = driver.load() # loads all documents from db into local Document objects
for document in documents:
print(document)
Add more keys into a document
from mongodriver.mongodriver import Driver
driver = Driver(
connection_url="mongodb+srv://example:SecurePassword@testcluster.e2lhq.mongodb.net/myFirstDatabase?retryWrites=true&w=majority",
db_name="example_db", collection_name="example_collection")
json_document = {"foo": 1, "bar": 2}
new_document = driver.create(json_document)
new_document.set({"new_val1": 15, "new_val2": 10})
print(new_document)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
mongodriver-1.0.9.tar.gz
(3.6 kB
view hashes)
Built Distribution
Close
Hashes for mongodriver-1.0.9-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cf7bfa46df0e60dba6633999acd28f1dcaf879ed6ce3243d34658af1831af3d7 |
|
MD5 | 1e928e86f3a7428b80f6b45919ec4143 |
|
BLAKE2b-256 | 7dae82752397f8d471043fd0abed16969a16be51961aa65b091905c957c28703 |