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

15 statements  

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

1import os 1abcdef

2 

3from mongodb_odm import Document, Field, apply_indexes, connect, disconnect 1abcdef

4 

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

6 

7 

8class User(Document): 1abcdef

9 name: str = Field() 1abcdef

10 email: str = Field() 1abcdef

11 

12 

13def main(): 1abcdef

14 # Connection 

15 connect(MONGO_URL) 1abcdef

16 apply_indexes() 1abcdef

17 

18 # Create document 

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

20 user.create() 1abcdef

21 

22 # Find documents 

23 _users = list(User.find({"name": "John"})) 1abcdef

24 

25 # Update document 

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

27 

28 # Delete document 

29 user.delete() 1abcdef

30 

31 # Cleanup 

32 disconnect() 1abcdef

33 

34 

35if __name__ == "__main__": 1abcdef

36 main()