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.2.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.2-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mangomango-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 655f756854a3aa7eb39b4c206f8bff974431abf9556fce5cf18b134d9714a76d
MD5 493e6a4778e42613c998682d356319ad
BLAKE2b-256 1bc504f5b8638173f696671ebbb4d172eb184a239dd39a43ef590f82d4df19ec

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mangomango-0.1.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c8b88403113d36a6d82620de2561a6978a1ff0ee1c81e801eeca3fcf4712cbf9
MD5 2794200e0ca59a47d1f5ee5e4885de08
BLAKE2b-256 55c8e5d23f5d8ef62e7f7c3cbb1aa2f0ca8ae507f2952551f0ce44c554d932bf

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