Quick Start
Installing
To start using Mongotoy just run to install it:
1 | |
Or, if using poetry:
1 | |
Quick Start
Let's get started with a minimal example by defining a document and performing CRUD(s) operations on the database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | |
This example demonstrates the usage of Mongotoy to interact with MongoDB databases in Python asynchronously.
Firstly, it defines a Person document class using Mongotoy's Document base class, specifying fields such
as name, last_name, and dob (date of birth).
Then, it creates a database engine using Engine('test-db'), indicating the name of the MongoDB database to connect to.
Within the main() function:
- A new Person instance named person is created with some sample data.
- The script connects to the MongoDB database using
await db.connect('mongodb://localhost:27017'). - A database session is opened using
async with db.session() as session. - The person object is saved to the database using
await session.save(person). - All Person objects are fetched from the database using
async for c in session.objects(Person), and their details are printed. - The
dobfield of the person object is updated and the updated person object is saved back to the database. - Finally, the person object is deleted from the database using
await session.delete(person).