Connect a MongoDB application to Azure Cosmos DB
APPLIES TO:
MongoDB
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 DB 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
Prerequisites
- An Azure account. If you don't have an Azure account, create a free Azure account now.
- An Azure Cosmos DB account. For instructions, see Build a web app using Azure Cosmos DB's API for MongoDB and .NET SDK.
Get the MongoDB connection string by using the quick start
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.
Get the MongoDB connection string to customize
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.
Connection string requirements
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): Azure Cosmos DB account name.
- Password (required): Azure Cosmos DB account password.
- Host (required): FQDN of the Azure Cosmos DB 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
Driver Requirements
All drivers that support wire protocol version 3.4 or greater will support Azure Cosmos DB for MongoDB.
Specifically, client drivers must support the Service Name Identification (SNI) TLS extension and/or the appName connection string option. If the appName
parameter is provided, it must be included as found in the connection string value in the Azure portal.
Next steps
- 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.
- Trying to do capacity planning for a migration to Azure Cosmos DB? You can use information about your existing database cluster for capacity planning.
- If all you know is the number of vcores and servers in your existing database cluster, read about estimating request units using vCores or vCPUs
- If you know typical request rates for your current database workload, read about estimating request units using Azure Cosmos DB capacity planner
Feedback
Submit and view feedback for