A simple MongoDB client that works with Pydantic models.
Project description
MangoMango
Basics
Usage
Connecting to MongoDB
To connect to a MongoDB instance, create a MangoMango client:
from mangomango import MangoMango
client = MangoMango(host="localhost", port=27017)
Creating a Database
To create or get a database, use the get_database method:
db = client.get_database("mydatabase")
Defining a Table
To define a table, use the table decorator on a Pydantic model. The type will not change, it will just register a table with this Model.
To get the table, use get_table:
@db.table(collection="users", primary_key="id")
class User(BaseModel):
id: int
username: str
email: str
UserTable = db.get_table("users")
Inserting Data
To insert data into a table:
user = User(id=0, username="john_doe", email="john@example.com")
UserTable.insert(user)
Querying Data
To query data from a table:
user = UserTable.find({"username": "john_doe"})
print(user.username, user.email)
Updating Data
To update data in a table, use the update() method. The first argument is the primary key of the object, it can also be the object itself.
UserTable.update(john.id, {"$set": {"email": "john_new@example.com"}})
You can also edit the object and re-push it with the same primary key. use push() method that insert if there is no object with the same primary key, else it remove and then insert
john.email = "john_new@example.com"
UserTable.push(john)
Deleting Data
To delete one data from a table, use remove() method. Can take either primary key or object as first argument:
UserTable.remove(john.id)
Bulk Insert
To insert multiple objects:
users = [
User(id=1, username="alice", email="alice@example.com"),
User(id=2, username="bob", email="bob@example.com")
]
UserTable.insert_many_iter(users)
Bulk Update
To update multiple objects:
UserTable.update_many({"username": {"$in": ["alice", "bob"]}}, {"$set": {"active": True}})
Bulk Delete
To delete multiple objects:
UserTable.remove_many({"username": {"$in": ["alice", "bob"]}})
the doc is not finished
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 mangomango-0.1.3.tar.gz.
File metadata
- Download URL: mangomango-0.1.3.tar.gz
- Upload date:
- Size: 3.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.0.0 CPython/3.13.1 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9d7ab7dafcec6e8b651a3fb9c94a167c2f0dad56bcb01636ab2193e650f43c42
|
|
| MD5 |
083e2fd35322bc64deffb517bf57d7cf
|
|
| BLAKE2b-256 |
830fcca455eda74b657265300117a6cb2f445c72c34e38d2fde873479079dbff
|
File details
Details for the file mangomango-0.1.3-py3-none-any.whl.
File metadata
- Download URL: mangomango-0.1.3-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.0.0 CPython/3.13.1 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
48624836b90d0f0c196b0aa15fc9961394d33d215db41247978632cb9821f51e
|
|
| MD5 |
10a6d33a918a20e88d4483ae1aedc13a
|
|
| BLAKE2b-256 |
b5673d6e25560657ac911a060aa9338398aca245c36f52de6bd0c79415c311ea
|