You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/cosmos-db/mongodb/how-to-python-get-started.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,14 +20,14 @@ This article shows you how to connect to Azure Cosmos DB for MongoDB using the P
20
20
> [!NOTE]
21
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
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/).
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
24
25
25
## Prerequisites
26
26
27
27
* An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free).
28
28
*[Python 3.8+](https://www.python.org/downloads/)
29
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)
30
+
*[Azure Cosmos DB for MongoDB resource](/azure/cosmos-db/nosql/quickstart-portal)
Get started with the PyMongo package to create databases, collections, and documents within your Azure Cosmos DB resource. Follow these steps to install the package and try out example code for basic tasks.
22
+
Get started with MongoDB to create databases, collections, and docs within your Azure Cosmos DB resource. Follow these steps to deploy a minimal solution to your environment using the Azure Developer CLI.
29
23
30
-
> [!NOTE]
31
-
> The [example code snippets](https://github.com/Azure-Samples/azure-cosmos-db-mongodb-python-getting-started) are available on GitHub as a Python project.
32
-
33
-
In this quickstart, you'll 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/). Also, you'll use the [MongoDB extension commands](./custom-commands.md), which are designed to help you create and obtain database resources that are specific to the [Azure Cosmos DB capacity model](../resource-model.md).
24
+
[API for MongoDB reference documentation](https://www.mongodb.com/docs/drivers/python-drivers/) | [pymongo package](https://pypi.org/project/pymongo/)
* In a terminal or command window, run `python --version` to check that you have a recent version of Python.
44
-
* Run ``az --version`` (Azure CLI) or `Get-Module -ListAvailable Az*` (Azure PowerShell) to check that you have the appropriate Azure command-line tools installed.
33
+
Deploy this project's development container to your environment. Then, use the Azure Developer CLI (`azd`) to create an Azure Cosmos DB for MongoDB account and deploy a containerized sample application. The sample application uses the client library to manage, create, read, and query sample data.
45
34
46
-
## Setting up
35
+
::: zone pivot="devcontainer-codespace"
47
36
48
-
This section walks you through creating an Azure Cosmos DB account and setting up a project that uses the MongoDB npm package.
37
+
[](https://codespaces.new/alexwolfmsft/cosmos-db-mongodb-python-quickstart?template=false&quickstart=1&azure-portal=true)
49
38
50
-
### Create an Azure Cosmos DB account
39
+
::: zone-end
51
40
52
-
This quickstart will create a single Azure Cosmos DB account using the API for MongoDB.
41
+
::: zone pivot="devcontainer-vscode"
53
42
54
-
#### [Azure CLI](#tab/azure-cli)
43
+
[](https://vscode.dev/redirect?url=vscode://ms-vscode-remote.remote-containers/cloneInVolume?url=https://github.com/alexwolfmsft/cosmos-db-mongodb-python-quickstart)
[!INCLUDE [Powershell - create resource group and resources](./includes/powershell-create-resource-group-and-resource.md)]
49
+
> [!IMPORTANT]
50
+
> GitHub accounts include an entitlement of storage and core hours at no cost. For more information, see [included storage and core hours for GitHub accounts](https://docs.github.com/billing/managing-billing-for-github-codespaces/about-billing-for-github-codespaces#monthly-included-storage-and-core-hours-for-personal-accounts).
1. Open a terminal in the root directory of the project.
69
59
70
-
#### [Azure CLI](#tab/azure-cli)
60
+
1. Authenticate to the Azure Developer CLI using `azd auth login`. Follow the steps specified by the tool to authenticate to the CLI using your preferred Azure credentials.
71
61
72
-
[!INCLUDE [Azure CLI - get connection string](./includes/azure-cli-get-connection-string.md)]
62
+
```azurecli
63
+
azd auth login
64
+
```
73
65
74
-
#### [PowerShell](#tab/azure-powershell)
66
+
1. Use `azd init` to initialize the project.
75
67
76
-
[!INCLUDE [Powershell - get connection string](./includes/powershell-get-connection-string.md)]
> This quickstart uses the [azure-samples/cosmos-db-mongodb-python-quickstart](https://github.com/alexwolfmsft/cosmos-db-mongodb-python-quickstart) template GitHub repository. The Azure Developer CLI automatically clones this project to your machine if it is not already there.
79
74
80
-
[!INCLUDE [Portal - get connection string](./includes/portal-get-connection-string-from-resource.md)]
75
+
1. During initialization, configure a unique environment name.
81
76
82
-
---
77
+
> [!TIP]
78
+
> The environment name will also be used as the target resource group name. For this quickstart, consider using `msdocs-cosmos-db`.
83
79
84
-
### Create a new Python app
80
+
1. Deploy the Azure Cosmos DB account using `azd up`. The Bicep templates also deploy a sample web application.
85
81
86
-
1. Create a new empty folder using your preferred terminal and change directory to the folder.
82
+
```azurecli
83
+
azd up
84
+
```
87
85
88
-
> [!NOTE]
89
-
> 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.
86
+
1. During the provisioning process, select your subscription and desired location. Wait for the provisioning process to complete. The process can take **approximately five minutes**.
87
+
88
+
1. Once the provisioning of your Azure resources is done, a URL to the running web application is included in the output.
SUCCESS: Your application was provisioned and deployed to Azure in 5 minutes 0 seconds.
97
+
```
98
+
99
+
1. Use the URL in the console to navigate to your web application in the browser. Observe the output of the running app.
90
100
91
-
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.
101
+
:::image type="content" source="media/quickstart-python/cosmos-python-app.png" alt-text="Screenshot of the running web application.":::
92
102
93
-
```text
103
+
---
104
+
105
+
### Install the client library
106
+
107
+
1. Create a `requirements.txt` file in your app directory that lists the [PyMongo](https://www.mongodb.com/docs/drivers/pymongo/) and [python-dotenv](https://pypi.org/project/python-dotenv/) packages.
108
+
109
+
```bash
94
110
# requirements.txt
95
111
pymongo
96
112
python-dotenv
97
113
```
98
114
99
-
3. Create a virtual environment and install the packages.
115
+
1. Create a virtual environment and install the packages.
116
+
117
+
## [Windows](#tab/windows-package)
100
118
101
-
#### [Windows](#tab/venv-windows)
102
-
103
119
```bash
104
120
# py -3 uses the global python interpreter. You can also use python3 -m venv .venv.
Use the [MongoClient](https://pymongo.readthedocs.io/en/stable/api/pymongo/mongo_client.html#pymongo.mongo_client.MongoClient) object to connect to your Azure Cosmos DB for MongoDB resource. The connect method returns a reference to the database.
0 commit comments