Coverage for docs_src / tutorial / find_raw / tutorial000.py: 100%
50 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 Optional 1abcdef
4from mongodb_odm import ASCENDING, Document, IndexModel, apply_indexes, connect 1abcdef
5from pymongo import MongoClient 1abcdef
8class Player(Document): 1abcdef
9 name: str 1abcdef
10 country_code: str 1abcdef
11 rating: Optional[int] = None 1abcdef
13 class ODMConfig(Document.ODMConfig): 1abcdef
14 indexes = [ 1abcdef
15 IndexModel([("rating", ASCENDING)]),
16 ]
19def configuration(): 1abcdef
20 connect(os.environ.get("MONGO_URL", "mongodb://localhost:27017/testdb")) 1abcdef
21 apply_indexes() 1abcdef
24def create_players(): 1abcdef
25 Player(name="Pelé", country_code="BRA", rating=98).create() 1abcdef
26 Player(name="Diego Maradona", country_code="ARG", rating=97).create() 1abcdef
27 Player(name="Zinedine Zidane", country_code="FRA", rating=94).create() 1abcdef
28 Player(name="Ronaldo", country_code="BRA", rating=94).create() 1abcdef
29 Player(name="Neymar", country_code="BRA", rating=89).create() 1abcdef
30 Player(name="Lionel Messi", country_code="ARG", rating=91).create() 1abcdef
31 Player(name="Ángel Di María", country_code="ARG", rating=84).create() 1abcdef
32 Player(name="Karim Benzema", country_code="FRA", rating=89).create() 1abcdef
33 Player(name="Antoine Griezmann", country_code="FRA", rating=85).create() 1abcdef
34 Player(name="Kylian Mbappé", country_code="FRA", rating=91).create() 1abcdef
35 Player(name="Gerd Müller", country_code="GER").create() 1abcdef
36 Player(name="Miroslav Klose", country_code="GER", rating=91).create() 1abcdef
37 Player(name="Thomas Müller", country_code="GER", rating=87).create() 1abcdef
38 Player(name="Cristiano Ronaldo", country_code="POR", rating=87).create() 1abcdef
39 Player(name="Eusébio", country_code="POR", rating=93).create() 1abcdef
40 Player(name="Diogo Jota", country_code="POR", rating=85).create() 1abcdef
41 Player(name="David Beckham", country_code="ENG", rating=89).create() 1abcdef
42 Player(name="Wayne Rooney", country_code="ENG", rating=80).create() 1abcdef
43 Player(name="Harry Kane", country_code="ENG", rating=89).create() 1abcdef
46def filter_documents(): 1abcdef
47 players = Player.find_raw(filter={Player.rating: {"$gte": 10}}) 1abcdef
48 for player in players: 1abcdef
49 print(player) 1abcdef
50 print() 1abcdef
53def pymongo_find_and_find_raw_comparison(): 1abcdef
54 filter = {Player.rating: {"$gte": 10}} 1abcdef
56 connection_url = os.environ.get("MONGO_URL", "mongodb://localhost:27017/testdb") 1abcdef
57 db = MongoClient(connection_url).get_database() 1abcdef
59 py_players = db.player.find(filter) 1abcdef
60 odm_players = Player.find_raw(filter) 1abcdef
62 print(py_players, odm_players) 1abcdef
65def main(): 1abcdef
66 configuration() 1abcdef
67 create_players() 1abcdef
69 filter_documents() 1abcdef
70 pymongo_find_and_find_raw_comparison() 1abcdef
73if __name__ == "__main__": 1abcdef
74 main()