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.0.tar.gz (3.8 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.0-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mangomango-0.1.0.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.5 Windows/10

File hashes

Hashes for mangomango-0.1.0.tar.gz
Algorithm Hash digest
SHA256 32c504c0157e828d33f63b24254fddb424da72f3c7a1de6ad4da43295c42f9b1
MD5 749b1aee4535dbfae927133df1f68de7
BLAKE2b-256 a3f0b3dd7340c6ccf8762d3c021ada9e8d4b6b28d7c4d119a5fdab5e5a481b8b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mangomango-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.5 Windows/10

File hashes

Hashes for mangomango-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2913ac68d14bbb661e2cf74733405d9cb4ce846adfd275b4789f19fc8a475e20
MD5 9b31b3ef2cbde189b3e21d23da3ab2c9
BLAKE2b-256 9f73496baf52d493b32f2509ef8b951b2f4943840a4fd4a6a7151006811ec70e

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