Coverage for docs_src / tutorial / get_document / tutorial000.py: 98%

48 statements  

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

1import os 1abcdef

2from typing import Optional 1abcdef

3 

4from mongodb_odm import ( 1abcdef

5 ASCENDING, 

6 DESCENDING, 

7 Document, 

8 IndexModel, 

9 apply_indexes, 

10 connect, 

11) 

12from mongodb_odm.exceptions import ObjectDoesNotExist 1abcdef

13 

14 

15class Player(Document): 1abcdef

16 name: str 1abcdef

17 country_code: str 1abcdef

18 rating: Optional[int] = None 1abcdef

19 

20 class ODMConfig(Document.ODMConfig): 1abcdef

21 indexes = [ 1abcdef

22 IndexModel([("rating", ASCENDING)]), 

23 ] 

24 

25 

26def configuration(): 1abcdef

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

28 apply_indexes() 1abcdef

29 

30 

31def create_players(): 1abcdef

32 Player(name="Pelé", country_code="BRA", rating=98).create() 1abcdef

33 Player(name="Diego Maradona", country_code="ARG", rating=97).create() 1abcdef

34 Player(name="Zinedine Zidane", country_code="FRA", rating=94).create() 1abcdef

35 Player(name="Ronaldo", country_code="BRA", rating=94).create() 1abcdef

36 Player(name="Neymar", country_code="BRA", rating=89).create() 1abcdef

37 Player(name="Lionel Messi", country_code="ARG", rating=91).create() 1abcdef

38 Player(name="Ángel Di María", country_code="ARG", rating=84).create() 1abcdef

39 Player(name="Karim Benzema", country_code="FRA", rating=89).create() 1abcdef

40 Player(name="Antoine Griezmann", country_code="FRA", rating=85).create() 1abcdef

41 Player(name="Kylian Mbappé", country_code="FRA", rating=91).create() 1abcdef

42 Player(name="Gerd Müller", country_code="GER").create() 1abcdef

43 Player(name="Miroslav Klose", country_code="GER", rating=91).create() 1abcdef

44 Player(name="Thomas Müller", country_code="GER", rating=87).create() 1abcdef

45 Player(name="Cristiano Ronaldo", country_code="POR", rating=87).create() 1abcdef

46 Player(name="Eusébio", country_code="POR", rating=93).create() 1abcdef

47 Player(name="Diogo Jota", country_code="POR", rating=85).create() 1abcdef

48 Player(name="David Beckham", country_code="ENG", rating=89).create() 1abcdef

49 Player(name="Wayne Rooney", country_code="ENG", rating=80).create() 1abcdef

50 Player(name="Harry Kane", country_code="ENG", rating=89).create() 1abcdef

51 

52 

53def get_document(): 1abcdef

54 try: 1abcdef

55 player = Player.get(filter={Player.country_code: "example@test.com"}) 1abcdef

56 print(player) 

57 except ObjectDoesNotExist as ex: 1abcdef

58 print(ex) 1abcdef

59 

60 

61def get_last_document(): 1abcdef

62 player = Player.get( 1abcdef

63 filter={Player.rating: {"$gte": 90}}, sort=[("_id", DESCENDING)] 

64 ) 

65 print(player) 1abcdef

66 

67 

68def main(): 1abcdef

69 configuration() 1abcdef

70 

71 get_document() 1abcdef

72 create_players() 1abcdef

73 

74 get_document() 1abcdef

75 get_last_document() 1abcdef

76 

77 

78if __name__ == "__main__": 1abcdef

79 main()