Coverage for docs_src / tutorial / get_or_create / tutorial000.py: 100%
51 statements
« prev ^ index » next coverage.py v7.12.0, created at 2025-12-07 08:37 +0000
« prev ^ index » next coverage.py v7.12.0, created at 2025-12-07 08:37 +0000
1import os 1abcdef
2from typing import Any, Optional 1abcdef
4from mongodb_odm import ASCENDING, Document, IndexModel, apply_indexes, connect 1abcdef
7class Player(Document): 1abcdef
8 name: str 1abcdef
9 country_code: str 1abcdef
10 rating: Optional[int] = None 1abcdef
12 class ODMConfig(Document.ODMConfig): 1abcdef
13 indexes = [ 1abcdef
14 IndexModel([("rating", ASCENDING)]),
15 ]
18def create_players(): 1abcdef
19 Player(name="Pelé", country_code="BRA", rating=98).create() 1abcdef
20 Player(name="Diego Maradona", country_code="ARG", rating=97).create() 1abcdef
21 Player(name="Zinedine Zidane", country_code="FRA", rating=94).create() 1abcdef
22 Player(name="Ronaldo", country_code="BRA", rating=94).create() 1abcdef
23 Player(name="Neymar", country_code="BRA", rating=89).create() 1abcdef
24 Player(name="Lionel Messi", country_code="ARG", rating=91).create() 1abcdef
25 Player(name="Ángel Di María", country_code="ARG", rating=84).create() 1abcdef
26 Player(name="Karim Benzema", country_code="FRA", rating=89).create() 1abcdef
27 Player(name="Antoine Griezmann", country_code="FRA", rating=85).create() 1abcdef
28 Player(name="Kylian Mbappé", country_code="FRA", rating=91).create() 1abcdef
29 Player(name="Gerd Müller", country_code="GER").create() 1abcdef
30 Player(name="Miroslav Klose", country_code="GER", rating=91).create() 1abcdef
31 Player(name="Thomas Müller", country_code="GER", rating=87).create() 1abcdef
32 Player(name="Cristiano Ronaldo", country_code="POR", rating=87).create() 1abcdef
33 Player(name="Eusébio", country_code="POR", rating=93).create() 1abcdef
34 Player(name="Diogo Jota", country_code="POR", rating=85).create() 1abcdef
35 Player(name="David Beckham", country_code="ENG", rating=89).create() 1abcdef
36 Player(name="Wayne Rooney", country_code="ENG", rating=80).create() 1abcdef
37 Player(name="Harry Kane", country_code="ENG", rating=89).create() 1abcdef
40def configuration(): 1abcdef
41 connect(os.environ.get("MONGO_URL", "mongodb://localhost:27017/testdb")) 1abcdef
42 apply_indexes() 1abcdef
45def general_process(): 1abcdef
46 filter: dict[str, Any] = { 1abcdef
47 Player.name: "Pelé",
48 Player.country_code: "example@test.com",
49 }
50 player = Player.find_one(filter) 1abcdef
51 created = False 1abcdef
52 if not player: 1abcdef
53 player = Player(**filter).create() 1abcdef
54 created = True 1abcdef
55 print(created, player) 1abcdef
58def get_or_create_document(): 1abcdef
59 player, created = Player.get_or_create( 1abcdef
60 filter={Player.name: "Pelé", Player.country_code: "BRA"}
61 )
62 print(created, player) 1abcdef
64 player, created = Player.get_or_create( 1abcdef
65 filter={Player.name: "Vini Jr.", Player.country_code: "BRA"}
66 )
67 print(created, player) 1abcdef
70def main(): 1abcdef
71 configuration() 1abcdef
73 general_process() 1abcdef
74 create_players() 1abcdef
76 general_process() 1abcdef
77 get_or_create_document() 1abcdef
80if __name__ == "__main__": 1abcdef
81 main()