|
| 1 | +--- |
| 2 | +title: Get started with Azure Cosmos DB for MongoDB and Python |
| 3 | +description: Get started developing a Python application that works with Azure Cosmos DB for MongoDB. This article helps you learn how to set up a project and configure access to an Azure Cosmos DB for MongoDB database. |
| 4 | +author: diberry |
| 5 | +ms.author: diberry |
| 6 | +ms.service: cosmos-db |
| 7 | +ms.reviewer: sidandrews |
| 8 | +ms.subservice: mongodb |
| 9 | +ms.devlang: python |
| 10 | +ms.topic: how-to |
| 11 | +ms.date: 11/16/2022 |
| 12 | +ms.custom: devx-track-js, ignite-2022 |
| 13 | +--- |
| 14 | + |
| 15 | +# Get started with Azure Cosmos DB for MongoDB and Python |
| 16 | +[!INCLUDE[MongoDB](../includes/appliesto-mongodb.md)] |
| 17 | + |
| 18 | +This article shows you how to connect to Azure Cosmos DB for MongoDB using the PyMongo driver package. Once connected, you can perform operations on databases, collections, and docs. |
| 19 | + |
| 20 | +> [!NOTE] |
| 21 | +> The [example code snippets](https://github.com/Azure-Samples/azure-cosmos-db-mongodb-python-getting-started) are available on GitHub as a Python project. |
| 22 | +
|
| 23 | +This article shows you how to communicate with the Azure Cosmos DB’s API for MongoDB by using one of the open-source MongoDB client drivers for Python, [PyMongo](https://www.mongodb.com/docs/drivers/pymongo/). |
| 24 | + |
| 25 | +## Prerequisites |
| 26 | + |
| 27 | +* An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free). |
| 28 | +* [Python 3.8+](https://www.python.org/downloads/) |
| 29 | +* [Azure Command-Line Interface (CLI)](/cli/azure/) or [Azure PowerShell](/powershell/azure/) |
| 30 | +* [Azure Cosmos DB for MongoDB resource](quickstart-python.md#create-an-azure-cosmos-db-account) |
| 31 | + |
| 32 | +## Create a new Python app |
| 33 | + |
| 34 | +1. Create a new empty folder using your preferred terminal and change directory to the folder. |
| 35 | + |
| 36 | + > [!NOTE] |
| 37 | + > If you just want the finished code, download or fork and clone the [example code snippets](https://github.com/Azure-Samples/azure-cosmos-db-mongodb-python-getting-started) repo that has the full example. You can also `git clone` the repo in Azure Cloud Shell to walk through the steps shown in this quickstart. |
| 38 | +
|
| 39 | +2. Create a *requirements.txt* file that lists the [PyMongo](https://www.mongodb.com/docs/drivers/pymongo/) and [python-dotenv](https://pypi.org/project/python-dotenv/) packages. The `dotenv` package is used to read the environment variables from a `.env` file during local development. |
| 40 | + |
| 41 | + ```text |
| 42 | + # requirements.txt |
| 43 | + pymongo |
| 44 | + python-dotenv |
| 45 | + ``` |
| 46 | +
|
| 47 | +3. Create a virtual environment and install the packages. |
| 48 | +
|
| 49 | + #### [Windows](#tab/venv-windows) |
| 50 | + |
| 51 | + ```bash |
| 52 | + # py -3 uses the global python interpreter. You can also use python3 -m venv .venv. |
| 53 | + py -3 -m venv .venv |
| 54 | + source .venv/Scripts/activate |
| 55 | + pip install -r requirements.txt |
| 56 | + ``` |
| 57 | + |
| 58 | + #### [Linux / macOS](#tab/venv-linux+macos) |
| 59 | + |
| 60 | + ```bash |
| 61 | + python3 -m venv .venv |
| 62 | + source .venv/bin/activate |
| 63 | + pip install -r requirements.txt |
| 64 | + ``` |
| 65 | + |
| 66 | + --- |
| 67 | +
|
| 68 | +## Connect with PyMongo driver to Azure Cosmos DB for MongoDB |
| 69 | +
|
| 70 | +To connect with the PyMongo driver to Azure Cosmos DB, create an instance of the [MongoClient](https://pymongo.readthedocs.io/en/stable/api/pymongo/mongo_client.html#pymongo.mongo_client.MongoClient) object. This class is the starting point to perform all operations against databases. |
| 71 | +
|
| 72 | +The most common constructor for **MongoClient** requires just the `host` parameter, which in this article is set to the `COSMOS_CONNECTION_STRING` environment variable. There are other optional parameters and keyword parameters you can use in the constructor. Many of the optional parameters can also be specified with the `host` parameter. If the same option is passed in with `host` and as a parameter, the parameter takes precedence. |
| 73 | +
|
| 74 | +Refer to the [Troubleshooting guide](error-codes-solutions.md) for connection issues. |
| 75 | +
|
| 76 | +## Get resource name |
| 77 | +
|
| 78 | +In the commands below, we show *msdocs-cosmos* as the resource group name. Change the name as appropriate for your situation. |
| 79 | +
|
| 80 | +### [Azure CLI](#tab/azure-cli) |
| 81 | +
|
| 82 | +[!INCLUDE [Azure CLI - get resource name](<./includes/azure-cli-get-resource-name.md>)] |
| 83 | +
|
| 84 | +### [PowerShell](#tab/azure-powershell) |
| 85 | +
|
| 86 | +[!INCLUDE [Powershell - set resource name](<./includes/powershell-set-resource-name.md>)] |
| 87 | +
|
| 88 | +### [Portal](#tab/azure-portal) |
| 89 | +
|
| 90 | +Skip this step and use the information for the portal in the next step. |
| 91 | +
|
| 92 | +--- |
| 93 | +## Retrieve your connection string |
| 94 | +
|
| 95 | +### [Azure CLI](#tab/azure-cli) |
| 96 | +
|
| 97 | +[!INCLUDE [Azure CLI - get connection string](<./includes/azure-cli-get-connection-string.md>)] |
| 98 | +
|
| 99 | +### [PowerShell](#tab/azure-powershell) |
| 100 | +
|
| 101 | +[!INCLUDE [Powershell - get connection string](<./includes/powershell-get-connection-string.md>)] |
| 102 | +
|
| 103 | +### [Portal](#tab/azure-portal) |
| 104 | +
|
| 105 | +[!INCLUDE [Portal - get connection string](<./includes/portal-get-connection-string-from-sign-in.md>)] |
| 106 | +
|
| 107 | +--- |
| 108 | +
|
| 109 | +## Configure environment variables |
| 110 | +
|
| 111 | +[!INCLUDE [Multitab - store connection string in environment variable](<./includes/environment-variables-connection-string.md>)] |
| 112 | +
|
| 113 | +## Create MongoClient with connection string |
| 114 | +
|
| 115 | +1. Add dependencies to reference the [PyMongo](https://www.mongodb.com/docs/drivers/pymongo/) and [python-dotenv](https://pypi.org/project/python-dotenv/) packages. |
| 116 | +
|
| 117 | + :::code language="python" source="~/azure-cosmos-db-mongodb-python-getting-started/101-client-connection-string/run.py" id="package_dependencies"::: |
| 118 | +
|
| 119 | +2. Define a new instance of the `MongoClient` class using the constructor and the connection string read from an environment variable. |
| 120 | +
|
| 121 | + :::code language="python" source="~/azure-cosmos-db-mongodb-python-getting-started/101-client-connection-string/run.py" id="client_credentials"::: |
| 122 | +
|
| 123 | +For more information on different ways to create a ``MongoClient`` instance, see [Making a Connection with MongoClient](https://pymongo.readthedocs.io/en/stable/tutorial.html#making-a-connection-with-mongoclient). |
| 124 | +
|
| 125 | +## Close the MongoClient connection |
| 126 | +
|
| 127 | +When your application is finished with the connection, remember to close it. That `.close()` call should be after all database calls are made. |
| 128 | +
|
| 129 | +```python |
| 130 | +client.close() |
| 131 | +``` |
| 132 | + |
| 133 | +## Use MongoDB client classes with Azure Cosmos DB for API for MongoDB |
| 134 | + |
| 135 | +Let's look at the hierarchy of resources in the API for MongoDB and the object model that's used to create and access these resources. The API for MongoDB creates resources in the following order: |
| 136 | + |
| 137 | +* [MongoClient](https://pymongo.readthedocs.io/en/stable/api/pymongo/mongo_client.html) - The first step when working with PyMongo is to create a MongoClient to connect to Azure Cosmos DB's API for MongoDB. The client object is used to configure and execute requests against the service. |
| 138 | + |
| 139 | +* [Database](https://pymongo.readthedocs.io/en/stable/api/pymongo/database.html) - Azure Cosmos DB's API for MongoDB can support one or more independent databases. |
| 140 | + |
| 141 | +* [Collection](https://pymongo.readthedocs.io/en/stable/api/pymongo/database.html) - A database can contain one or more collections. A collection is a group of documents stored in MongoDB, and can be thought of as roughly the equivalent of a table in a relational database. |
| 142 | + |
| 143 | +* [Document](https://pymongo.readthedocs.io/en/stable/tutorial.html#documents) - A document is a set of key-value pairs. Documents have dynamic schema. Dynamic schema means that documents in the same collection don't need to have the same set of fields or structure. And common fields in a collection's documents may hold different types of data. |
| 144 | + |
| 145 | +To learn more about the hierarchy of entities, see the [Azure Cosmos DB resource model](../account-databases-containers-items.md) article. |
| 146 | + |
| 147 | +## See also |
| 148 | + |
| 149 | +- [PyPI Package](https://pypi.org/project/azure-cosmos/) |
| 150 | +- [API reference](https://www.mongodb.com/docs/drivers/python/) |
| 151 | + |
| 152 | +## Next steps |
| 153 | + |
| 154 | +Now that you've connected to an API for MongoDB account, use the next guide to create and manage databases. |
| 155 | + |
| 156 | +> [!div class="nextstepaction"] |
| 157 | +> [Create a database in Azure Cosmos DB for MongoDB using Python](how-to-python-manage-databases.md) |
0 commit comments