Coverage for docs_src / tutorial / find_one_document / tutorial000.py: 100%

43 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) 

12 

13 

14class Player(Document): 1abcdef

15 name: str 1abcdef

16 country_code: str 1abcdef

17 rating: Optional[int] = None 1abcdef

18 

19 class ODMConfig(Document.ODMConfig): 1abcdef

20 indexes = [ 1abcdef

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

22 ] 

23 

24 

25def configuration(): 1abcdef

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

27 apply_indexes() 1abcdef

28 

29 

30def create_players(): 1abcdef

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

50 

51 

52def find_one_document(): 1abcdef

53 player = Player.find_one() 1abcdef

54 print(player) 1abcdef

55 

56 

57def find_last_document(): 1abcdef

58 player = Player.find_one(sort=[("_id", DESCENDING)]) 1abcdef

59 print(player) 1abcdef

60 

61 

62def main(): 1abcdef

63 configuration() 1abcdef

64 create_players() 1abcdef

65 

66 find_one_document() 1abcdef

67 find_last_document() 1abcdef

68 

69 

70if __name__ == "__main__": 1abcdef

71 main()