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:

Prerequisites

There are two items required

  • An Azure account.

Get the MongoDB connection string by using the quick start

Follow the below steps:

  1. In an Internet browser, sign in to the Azure portal.

  2. In the Azure Cosmos DB pane, select the API.

  3. In the left pane of the account pane, select Quick start.

  4. 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. Comment on what you'd like to see. To learn how to craft your own connection, read Get the account's connection string information.

  5. Copy and paste the code snippet into your MongoDB app.

Get the MongoDB connection string to customize

Follow the below steps:

  1. In an Internet browser, sign in to the Azure portal.

  2. In the Azure Cosmos DB pane, select the API.

  3. In the left pane of the account pane, select Connection strings.

  4. The Connection strings pane 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. The connection string format is:

    mongodb://username:password@host:port/[database]?ssl=true

    The values of this string are:

    • 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 strings pane. 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 support Azure Cosmos DB for MongoDB.

    • 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.