Skip to content

Find One Document

Database

We will start with the same database structure and the same amount of data as previously.

Find One

Sometimes we need to find one object. In that scenario, we will use the classmethod find_one.

The find_one is a classmethod that returns a single object or None if the object does not exist.

# Code omitted above

def find_one_document():
    player = Player.find_one()
    print(player)

# Code omitted below
Full file preview
import os
from typing import Optional

from mongodb_odm import (
    ASCENDING,
    DESCENDING,
    Document,
    IndexModel,
    apply_indexes,
    connect,
)


class Player(Document):
    name: str
    country_code: str
    rating: Optional[int] = None

    class ODMConfig(Document.ODMConfig):
        indexes = [
            IndexModel([("rating", ASCENDING)]),
        ]


def configuration():
    connect(os.environ.get("MONGO_URL", "mongodb://localhost:27017/testdb"))
    apply_indexes()


def create_players():
    Player(name="Pelé", country_code="BRA", rating=98).create()
    Player(name="Diego Maradona", country_code="ARG", rating=97).create()
    Player(name="Zinedine Zidane", country_code="FRA", rating=94).create()
    Player(name="Ronaldo", country_code="BRA", rating=94).create()
    Player(name="Neymar", country_code="BRA", rating=89).create()
    Player(name="Lionel Messi", country_code="ARG", rating=91).create()
    Player(name="Ángel Di María", country_code="ARG", rating=84).create()
    Player(name="Karim Benzema", country_code="FRA", rating=89).create()
    Player(name="Antoine Griezmann", country_code="FRA", rating=85).create()
    Player(name="Kylian Mbappé", country_code="FRA", rating=91).create()
    Player(name="Gerd Müller", country_code="GER").create()
    Player(name="Miroslav Klose", country_code="GER", rating=91).create()
    Player(name="Thomas Müller", country_code="GER", rating=87).create()
    Player(name="Cristiano Ronaldo", country_code="POR", rating=87).create()
    Player(name="Eusébio", country_code="POR", rating=93).create()
    Player(name="Diogo Jota", country_code="POR", rating=85).create()
    Player(name="David Beckham", country_code="ENG", rating=89).create()
    Player(name="Wayne Rooney", country_code="ENG", rating=80).create()
    Player(name="Harry Kane", country_code="ENG", rating=89).create()


def find_one_document():
    player = Player.find_one()
    print(player)


def find_last_document():
    player = Player.find_one(sort=[("_id", DESCENDING)])
    print(player)


def main():
    configuration()
    create_players()

    find_one_document()
    find_last_document()


if __name__ == "__main__":
    main()

If the filter matches the collection data, then it will return a Player type object. Otherwise, it will return None.

The find_one will also act as find_first.

Find Last

To find the last document from the collection, we also use find_last.

# Code omitted above

def find_last_document():
    player = Player.find_one(sort=[("_id", DESCENDING)])
    print(player)

# Code omitted below
Full file preview
import os
from typing import Optional

from mongodb_odm import (
    ASCENDING,
    DESCENDING,
    Document,
    IndexModel,
    apply_indexes,
    connect,
)


class Player(Document):
    name: str
    country_code: str
    rating: Optional[int] = None

    class ODMConfig(Document.ODMConfig):
        indexes = [
            IndexModel([("rating", ASCENDING)]),
        ]


def configuration():
    connect(os.environ.get("MONGO_URL", "mongodb://localhost:27017/testdb"))
    apply_indexes()


def create_players():
    Player(name="Pelé", country_code="BRA", rating=98).create()
    Player(name="Diego Maradona", country_code="ARG", rating=97).create()
    Player(name="Zinedine Zidane", country_code="FRA", rating=94).create()
    Player(name="Ronaldo", country_code="BRA", rating=94).create()
    Player(name="Neymar", country_code="BRA", rating=89).create()
    Player(name="Lionel Messi", country_code="ARG", rating=91).create()
    Player(name="Ángel Di María", country_code="ARG", rating=84).create()
    Player(name="Karim Benzema", country_code="FRA", rating=89).create()
    Player(name="Antoine Griezmann", country_code="FRA", rating=85).create()
    Player(name="Kylian Mbappé", country_code="FRA", rating=91).create()
    Player(name="Gerd Müller", country_code="GER").create()
    Player(name="Miroslav Klose", country_code="GER", rating=91).create()
    Player(name="Thomas Müller", country_code="GER", rating=87).create()
    Player(name="Cristiano Ronaldo", country_code="POR", rating=87).create()
    Player(name="Eusébio", country_code="POR", rating=93).create()
    Player(name="Diogo Jota", country_code="POR", rating=85).create()
    Player(name="David Beckham", country_code="ENG", rating=89).create()
    Player(name="Wayne Rooney", country_code="ENG", rating=80).create()
    Player(name="Harry Kane", country_code="ENG", rating=89).create()


def find_one_document():
    player = Player.find_one()
    print(player)


def find_last_document():
    player = Player.find_one(sort=[("_id", DESCENDING)])
    print(player)


def main():
    configuration()
    create_players()

    find_one_document()
    find_last_document()


if __name__ == "__main__":
    main()

The find_one method accepts a sort object.

The sort kwargs accepts several types of data.

The data type for sort is Union[str, Sequence[tuple[str, Union[int, str, Mapping[str, Any]]]]].