Initiate Database and Define Model¶
Initiate Database and Create Collection¶
There is no need to create a database or collection separately. MongoDB automatically creates a new database and collection while we insert new data.
You need to define a Pydantic model that should inherit Document from mongodb_odm. When new insertion takes place, MongoDB automatically creates everything if something does not exist.
Target¶
- First, we will create a Player Document model class.
- Define indexes.
- Make sure everything is working and has an impact on the database.
Here's a collection structure that we want to achieve.
{
"_id": ObjectId('id'),
"name": "Pelé",
"country_code": "BRA",
"rating": null
}
{
"_id": ObjectId('id'),
"name": "Diego Maradona",
"country_code": "ARG",
"rating": 97
}
{
"_id": ObjectId('id'),
"name": "Zinedine Zidane",
"country_code": "FRA",
"rating": 96
}
Create the Document Model Class¶
The first thing we need to do is create a class to represent the data in the collection.
from typing import Optional
from mongodb_odm import Document
class Player(Document):
name: str
country_code: str
rating: Optional[int] = None
# Code omitted below
Full file preview
from typing import Optional
from mongodb_odm import Document
class Player(Document):
name: str
country_code: str
rating: Optional[int] = None
class ODMConfig(Document.ODMConfig):
collection_name = "player"
Import Document from mongodb_odm. Define the Player model that should use Document as the parent class.
Define the collections and fields¶
The next step is to define the fields of the class by using standard Python type annotations.
The name of each of these variables will be the name of the field in the collection. And the type of each of them will also be the type of collection field.
from typing import Optional
from mongodb_odm import Document
class Player(Document):
name: str
country_code: str
rating: Optional[int] = None
# Code omitted below
Full file preview
from typing import Optional
from mongodb_odm import Document
class Player(Document):
name: str
country_code: str
rating: Optional[int] = None
class ODMConfig(Document.ODMConfig):
collection_name = "player"
Field declaration¶
- Start with the field
namedefined as type str, which is the standard way to declare something with type in Python. - We declare
country_codewith typestrand in our database it will be stored as astringtype. - In the end, we define
rating. Notice that it has a type ofOptional[int]. And we import thatOptionalfrom thetypingstandard module. Theratingfield can benullorintin the database.
Primary key¶
- You may notice we did not define the primary key.
- We use MongoDB default primary key
_idas our primary key. - You can access
idand_id (PrivateAttr)both should share the same value.
Model ODMConfig¶
The ODMConfig class inside the Player is a special class.
We will use this ODMConfig class to change the configuration of the Player class.
from typing import Optional
from mongodb_odm import Document
class Player(Document):
name: str
country_code: str
rating: Optional[int] = None
class ODMConfig(Document.ODMConfig):
collection_name = "player"
Custom Collection Name¶
By default, the collection name of the Player model will be player. The model name will be converted to snake case.
But we can customize the default behavior by defining collection_name in the ODMConfig class.
from typing import Optional
from mongodb_odm import Document
class Player(Document):
name: str
country_code: str
rating: Optional[int] = None
class ODMConfig(Document.ODMConfig):
collection_name = "player"