Skip to main content

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

mangomango-0.1.1.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mangomango-0.1.1-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file mangomango-0.1.1.tar.gz.

File metadata

  • Download URL: mangomango-0.1.1.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

Hashes for mangomango-0.1.1.tar.gz
Algorithm Hash digest
SHA256 38d99a5d21a382e2318bb73747aa35fa2be47d8825059ae670b5ae6145ce7663
MD5 5f767a5a521d1373c5df29b586f2fb26
BLAKE2b-256 687a2a6f3f8ba68e623d1315066717b7add2f34e87ce19073f95cb8be67415b2

See more details on using hashes here.

File details

Details for the file mangomango-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: mangomango-0.1.1-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

Hashes for mangomango-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c596e4fbe36c70655f13a41da090ba0d3cac2a716b42834aceecfb5803b009ff
MD5 697bbd4a80c14e526391823d7d3763de
BLAKE2b-256 6dbf47b4f6287476606e078f821b58ae1dcc0aad24043db5a590d8c40342c81b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page