Skip to content

[Docs+] Getting Started With MongoDB and FastAPI #250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 33 commits into
base: master
Choose a base branch
from

Conversation

rachel-mack
Copy link
Collaborator

@rachel-mack rachel-mack commented Apr 8, 2025

Pull Request Info

PR Reviewing Guidelines

JIRA - https://jira.mongodb.org/browse/DOCSP-48895
original URL: https://www.mongodb.com/developer/languages/python/python-quickstart-fastapi/

Updating tutorial to use Pymongo Async: mongodb-developer/mongodb-with-fastapi#19

Staging Links

  • integrations/fastapi-integration
  • connect
  • integrations
  • Self-Review Checklist

    • Is this free of any warnings or errors in the RST?
    • Did you run a spell-check?
    • Did you run a grammar-check?
    • Are all the links working?
    • Are the facets and meta keywords accurate?
    • Are the page titles greater than 20 characters long and SEO relevant?

    Copy link

    netlify bot commented Apr 8, 2025

    Deploy Preview for docs-pymongo ready!

    Name Link
    🔨 Latest commit c42b82e
    🔍 Latest deploy log https://app.netlify.com/projects/docs-pymongo/deploys/68425e8cc67a060008520e95
    😎 Deploy Preview https://deploy-preview-250--docs-pymongo.netlify.app
    📱 Preview on mobile
    Toggle QR Code...

    QR Code

    Use your smartphone camera to open QR code link.

    To edit notification comments on pull requests, go to your Netlify project configuration.

    @rachel-mack rachel-mack marked this pull request as ready for review May 2, 2025 16:40
    @rustagir rustagir self-requested a review May 5, 2025 13:47
    Copy link
    Collaborator

    @rustagir rustagir left a comment

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    I reviewed 2/3 of the PR but there are a lot of formatting and style guide issues that It would be good for you to correct before I take a second pass. Let me know if you have any questions!

    uvicorn app:app --reload

    .. image:: /includes/integrations/fastapi-terminal.png
    :alt: Screenshot of terminal running FastAPI
    Copy link
    Collaborator

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    This step has a lot of indentation errors


    All the code for the example application is stored in ``app.py``.

    Connect to your MongoDB Atlas cluster using the asynchronous `Pymongo Driver <https://www.mongodb.com/docs/languages/python/pymongo-driver/current>`__, then specify the database named ``college``:
    Copy link
    Collaborator

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Since this page is now in the pymongo docs, change this link out for something that makes more sense (and use a ref)

    Comment on lines 113 to 117
    FastAPI encodes and decodes data as JSON strings, which do not support all of the
    data types that MongoDB's BSON data type can store. BSON has support for
    additional non-JSON-native data types, including ``ObjectId`` which is used for
    the default UUID attribute, ``_id``. Because of this, you must convert
    ``ObjectId`` objects to strings before storing them in the ``_id`` field.
    Copy link
    Collaborator

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    S: fix indentation

    Comment on lines 126 to 127
    # Represents an ObjectId field in the database.
    # It will be represented as a `str` on the model so that it can be
    Copy link
    Collaborator

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    S: move code to file where possible


    This is the primary model we use as the `response model <https://fastapi.tiangolo.com/tutorial/response-model/>`__ for the majority of our endpoints.

    I want to draw attention to the ``id`` field on this model. MongoDB uses ``_id``, but in Python, underscores at the start of attributes have special meaning. If you have an attribute on your model that starts with an underscore, `pydantic <https://pydantic-docs.helpmanual.io/>`__—the data validation framework used by FastAPI—will assume that it is a private variable, meaning you will not be able to assign it a value! To get around this, we name the field ``id`` but give it an alias of ``_id``. You also need to set ``populate_by_name`` to ``True`` in the model's ``model_config``
    Copy link
    Collaborator

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    S: fix for lots of style guide errors

    @rachel-mack rachel-mack requested a review from rustagir May 7, 2025 14:56
    Copy link
    Collaborator

    @rustagir rustagir left a comment

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    still a lot of issues to resolve, I am going OOO so perhaps another team member can pick this up!

    Comment on lines 319 to 320
    The application has two read routes: one for viewing all students, and one
    for viewing an individual student specified by their ``id``.
    Copy link
    Collaborator

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    S: convert into a list

    Copy link
    Collaborator Author

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Since it's only two items, I'm going to leave it as a paragraph.

    Comment on lines 343 to 345
    recommend using the `skip and limit parameters
    <https://pymongo.readthedocs.io/en/stable/api/pymongo/asynchronous/collection.html#pymongo.asynchronous.collection.AsyncCollection.find>`__
    in ``find`` to paginate your results.
    Copy link
    Collaborator

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    S: not sure why formatting is off here - also use __ at the end of urls (2 underscores)

    @rachel-mack rachel-mack requested a review from rustagir May 21, 2025 17:54
    Comment on lines 42 to 45
    - Python v3.9.0 or later
    - A MongoDB Atlas cluster
    To learn how to set up a cluster, see
    the :ref:`Getting Started <pymongo-get-started>`__ guide for more information.
    Copy link
    Collaborator

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Add punctuation to these list items

    Suggested change
    - Python v3.9.0 or later
    - A MongoDB Atlas cluster
    To learn how to set up a cluster, see
    the :ref:`Getting Started <pymongo-get-started>`__ guide for more information.
    - Python v3.9.0 or later.
    - A MongoDB Atlas cluster.
    To learn how to set up a cluster, see
    the :ref:`Getting Started <pymongo-get-started>`__ guide for more information.


    .. step:: Clone the example code example.

    Run the following command in your terminal to clone the code from the `mongodb-with-fastapi <https://github.com/mongodb-developer/mongodb-with-fastapi>`__ GitHub repository:
    Copy link
    Collaborator

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Suggested change
    Run the following command in your terminal to clone the code from the `mongodb-with-fastapi <https://github.com/mongodb-developer/mongodb-with-fastapi>`__ GitHub repository:
    Run the following command in your terminal to clone the code from the :github:`mongodb-with-fastapi <mongodb-developer/mongodb-with-fastapi>` GitHub repository:

    Comment on lines 67 to 70
    Installing your Python dependencies in a `virtualenv
    <https://docs.python.org/3/tutorial/venv.html>`__ allows you to install
    versions of your libraries for individual
    projects. Before running any ``pip`` commands, ensure your ``virtualenv`` is active.
    Copy link
    Collaborator

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    S: needs to be indented more

    versions of your libraries for individual
    projects. Before running any ``pip`` commands, ensure your ``virtualenv`` is active.

    Run the following command in your terminal to install the dependencies listed in the ``requirements.txt`` file:
    Copy link
    Collaborator

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    S: i think all this content isnt indented to the right level under the step

    Comment on lines 87 to 95
    .. code-block:: shell

    export MONGODB_URL="mongodb+srv://<username>:<password>@<url>/<db>?retryWrites=true&w=majority"

    .. tip:: Reset Environment Variables

    Anytime you start a new terminal session, you will must reset this
    environment variable. You can use `direnv <https://direnv.net/>`__ to
    make this process easier.
    Copy link
    Collaborator

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    S: fix indentation

    """
    A container holding a list of `StudentModel` instances.

    This exists because providing a top-level array in a JSON response can be a `vulnerability <https://haacked.com/archive/2009/06/25/json-hijacking.aspx/>`__
    Copy link
    Collaborator

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    S: this link isnt rendering properly. Consider moving this outside the code example as a note

    Comment on lines +308 to +311
    @app.post(
    "/students/",
    response_description="Add new student",
    response_model=StudentModel,
    Copy link
    Collaborator

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    S: this code block indentation is off

    Comment on lines 331 to 332
    This application has two read routes: one for viewing all students, and one
    for viewing an individual student specified by their ``id``.
    Copy link
    Collaborator

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    S: this type of "list sentence" is not style guide adherent

    Comment on lines 355 to 358
    This example uses the ``to_list()`` method; but in a real application,
    we recommend using the `skip and limit parameters
    <https://pymongo.readthedocs.io/en/stable/api/pymongo/asynchronous/collection.html#pymongo.asynchronous.collection.AsyncCollection.find>`__
    in ``find`` to paginate your results.
    Copy link
    Collaborator

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    S: use the api docs source constant to shorten this URL

    Comment on lines 390 to 392
    The ``update_student`` route is like a combination of the
    ``create_student`` and the ``show_student`` routes. It receives the ``id``
    of the student to update, and the new data in the JSON body.
    Copy link
    Collaborator

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Suggested change
    The ``update_student`` route is like a combination of the
    ``create_student`` and the ``show_student`` routes. It receives the ``id``
    of the student to update, and the new data in the JSON body.
    The ``update_student`` route functions similar to a combination of the
    ``create_student`` and the ``show_student`` routes. It receives the ``id``
    of the student to update, and the new data in the JSON body.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    None yet
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants