title | description | author | ms.author | ms.service | ms.subservice | ms.topic | ms.date | ms.reviewer |
---|---|---|---|---|---|---|---|---|
Connect a MongoDB application to Azure Cosmos DB |
Learn how to connect a MongoDB app to Azure Cosmos DB by getting the connection string from Azure portal |
timsander1 |
tisande |
cosmos-db |
cosmosdb-mongo |
how-to |
03/19/2020 |
sngun |
Learn how to connect your MongoDB app to an Azure Cosmos DB by using a MongoDB connection string. You can then use an Azure Cosmos database as the data store for your MongoDB app.
This tutorial provides two ways to retrieve connection string information:
- The quickstart method, for use with .NET, Node.js, MongoDB Shell, Java, and Python drivers
- The custom connection string method, for use with other drivers
- An Azure account. If you don't have an Azure account, create a free Azure account now.
- A Cosmos account. For instructions, see Build a web app using Azure Cosmos DB's API for MongoDB and .NET SDK.
-
In an Internet browser, sign in to the Azure portal.
-
In the Azure Cosmos DB blade, select the API.
-
In the left pane of the account blade, click Quick start.
-
Choose your platform (.NET, Node.js, MongoDB Shell, Java, Python). If you don't see your driver or tool listed, don't worry--we continuously document more connection code snippets. Please comment below on what you'd like to see. To learn how to craft your own connection, read Get the account's connection string information.
-
Copy and paste the code snippet into your MongoDB app.
:::image type="content" source="./media/connect-mongodb-account/QuickStartBlade.png" alt-text="Quick start blade":::
-
In an Internet browser, sign in to the Azure portal.
-
In the Azure Cosmos DB blade, select the API.
-
In the left pane of the account blade, click Connection String.
-
The Connection String blade opens. It has all the information necessary to connect to the account by using a driver for MongoDB, including a preconstructed connection string.
:::image type="content" source="./media/connect-mongodb-account/ConnectionStringBlade.png" alt-text="Connection String blade" lightbox= "./media/connect-mongodb-account/ConnectionStringBlade.png" :::
Important
Azure Cosmos DB has strict security requirements and standards. Azure Cosmos DB accounts require authentication and secure communication via TLS.
Azure Cosmos DB supports the standard MongoDB connection string URI format, with a couple of specific requirements: Azure Cosmos DB accounts require authentication and secure communication via TLS. So, the connection string format is:
mongodb://username:password@host:port/[database]?ssl=true
The values of this string are available in the Connection String blade shown earlier:
- Username (required): Cosmos account name.
- Password (required): Cosmos account password.
- Host (required): FQDN of the Cosmos account.
- Port (required): 10255.
- Database (optional): The database that the connection uses. If no database is provided, the default database is "test."
- ssl=true (required)
For example, consider the account shown in the Connection String blade. A valid connection string is:
mongodb://contoso123:0Fc3IolnL12312asdfawejunASDF@asdfYXX2t8a97kghVcUzcDv98hawelufhawefafnoQRGwNj2nMPL1Y9qsIr9Srdw==@contoso123.documents.azure.com:10255/mydatabase?ssl=true
- Learn how to use Studio 3T with Azure Cosmos DB's API for MongoDB.
- Learn how to use Robo 3T with Azure Cosmos DB's API for MongoDB.
- Explore MongoDB samples with Azure Cosmos DB's API for MongoDB.