Coverage for docs_src / async_tutorial / async_example.py: 100%

17 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2025-12-07 08:37 +0000

1import asyncio 1abcdef

2import os 1abcdef

3 

4from mongodb_odm import Document, Field, adisconnect, async_apply_indexes, connect 1abcdef

5 

6MONGO_URL = os.environ.get("MONGO_URL", "mongodb://localhost:27017/testdb") 1abcdef

7 

8 

9class User(Document): 1abcdef

10 name: str = Field() 1abcdef

11 email: str = Field() 1abcdef

12 

13 

14async def main(): 1abcdef

15 connect(MONGO_URL, async_is_enabled=True) 1abcdef

16 await async_apply_indexes() 1abcdef

17 

18 # Create document 

19 user = User(name="John", email="john@example.com") 1abcdef

20 await user.acreate() 1abcdef

21 

22 # Find documents 

23 _users = [user async for user in User.afind({"name": "John"})] 1abcdef

24 

25 # Update document 

26 await user.aupdate({"$set": {"name": "Jane"}}) 1abcdef

27 

28 # Delete document 

29 await user.adelete() 1abcdef

30 

31 # Cleanup 

32 await adisconnect() 1abcdef

33 

34 

35asyncio.run(main()) 1abcdef