将 MongoDB 应用程序连接到 Azure Cosmos DBConnect a MongoDB application to Azure Cosmos DB

了解如何使用 MongoDB 连接字符串,将 MongoDB 应用连接到 Azure Cosmos DB。Learn how to connect your MongoDB app to an Azure Cosmos DB by using a MongoDB connection string. 然后,可使用 Azure Cosmos 数据库作为 MongoDB 应用的数据存储。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:

先决条件Prerequisites

使用快速启动方法获取 MongoDB 连接字符串Get the MongoDB connection string by using the quick start

  1. 在 Internet 浏览器中,登录 Azure 门户In an Internet browser, sign in to the Azure portal.

  2. 在 Azure Cosmos DB 边栏选项卡中,选择 API 。In the Azure Cosmos DB blade, select the API.

  3. 在“帐户”边栏选项卡左侧窗格中,单击“快速启动” 。In the left pane of the account blade, click Quick start.

  4. 选择平台( .NETNode.jsMongoDB ShellJavaPython)。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.

  5. 将代码片段复制粘贴到 MongoDB 应用。Copy and paste the code snippet into your MongoDB app.

    “快速启动”边栏选项卡

获取要自定义的 MongoDB 连接字符串Get the MongoDB connection string to customize

  1. 在 Internet 浏览器中,登录 Azure 门户In an Internet browser, sign in to the Azure portal.

  2. 在 Azure Cosmos DB 边栏选项卡中,选择 API 。In the Azure Cosmos DB blade, select the API.

  3. 在“帐户”边栏选项卡的左侧窗格中,单击“连接字符串” 。In the left pane of the account blade, click Connection String.

  4. 此时将打开“连接字符串”边栏选项卡 。The Connection String blade opens. 其中显示了使用 MongoDB 驱动程序连接到帐户所需的所有信息,包括预先构造的连接字符串。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 具有严格的安全要求和标准。Azure Cosmos DB has strict security requirements and standards. Azure Cosmos DB 帐户需要通过 SSL 进行身份验证和安全通信。Azure Cosmos DB accounts require authentication and secure communication via SSL.

Azure Cosmos DB 支持标准的 MongoDB 连接字符串 URI 格式,但要满足几个具体的要求:Azure Cosmos DB 帐户需要通过 SSL 进行身份验证和安全通信。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 SSL. 因此,连接字符串格式为: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:

  • 用户名(必需):Cosmos 帐户名。Username (required): Cosmos account name.
  • 密码(必需):Cosmos 帐户密码。Password (required): Cosmos account password.
  • 主机(必需):Cosmos 帐户的 FQDN。Host (required): FQDN of the Cosmos account.
  • 端口(必需):10255。Port (required): 10255.
  • 数据库(可选):连接使用的数据库。Database (optional): The database that the connection uses. 如果未提供任何数据库,则默认数据库是“test”。If no database is provided, the default database is "test."
  • ssl=true(必需)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.cn:10255/mydatabase?ssl=true

后续步骤Next steps

  • 了解如何将 Studio 3T 与 Azure Cosmos DB 的用于 MongoDB 的 API 配合使用。Learn how to use Studio 3T with Azure Cosmos DB's API for MongoDB.
  • 了解如何将 Robo 3T 与 Azure Cosmos DB 的用于 MongoDB 的 API 配合使用。Learn how to use Robo 3T with Azure Cosmos DB's API for MongoDB.
  • 通过 Azure Cosmos DB 的用于 MongoDB 的 API 来浏览 MongoDB 示例Explore MongoDB samples with Azure Cosmos DB's API for MongoDB.