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

61 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 ASCENDING, Document, IndexModel, apply_indexes, connect 1abcdef

5 

6 

7class Player(Document): 1abcdef

8 name: str 1abcdef

9 country_code: str 1abcdef

10 rating: Optional[int] = None 1abcdef

11 

12 class ODMConfig(Document.ODMConfig): 1abcdef

13 indexes = [ 1abcdef

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

15 ] 

16 

17 

18def configuration(): 1abcdef

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

20 apply_indexes() 1abcdef

21 

22 

23def create_players(): 1abcdef

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

43 

44 

45def limit_data(): 1abcdef

46 players = Player.find(limit=2) 1abcdef

47 for player in players: 1abcdef

48 print(player) 1abcdef

49 print() 1abcdef

50 

51 

52def skip_data(): 1abcdef

53 players = Player.find(skip=2) 1abcdef

54 for player in players: 1abcdef

55 print(player) 1abcdef

56 print() 1abcdef

57 

58 

59def skip_and_limit_data(): 1abcdef

60 players = Player.find(skip=3, limit=2) 1abcdef

61 for player in players: 1abcdef

62 print(player) 1abcdef

63 print() 1abcdef

64 

65 

66def implement_pagination(page, limit): 1abcdef

67 skip = (page - 1) * limit 1abcdef

68 filter = {Player.rating: {"$gte": 90}} 1abcdef

69 players = Player.find(filter=filter, skip=skip, limit=limit) 1abcdef

70 for player in players: 1abcdef

71 print(player) 1abcdef

72 print() 1abcdef

73 

74 

75def main(): 1abcdef

76 configuration() 1abcdef

77 create_players() 1abcdef

78 

79 limit_data() 1abcdef

80 skip_data() 1abcdef

81 skip_and_limit_data() 1abcdef

82 implement_pagination(page=1, limit=10) 1abcdef

83 

84 

85if __name__ == "__main__": 1abcdef

86 main()