你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
快速入门:将 Azure Cosmos DB for MongoDB (RU) 与 .NET 配合使用
在本快速入门中,你将使用 Python 部署基本的 Azure Cosmos DB for MongoDB 应用程序。 Azure Cosmos DB for MongoDB 是一种无架构数据存储,允许应用程序使用 MongoDB 库在云中存储结构化表数据。 你将了解如何使用 Python 在 Azure Cosmos DB 资源中创建文档并执行基本任务。
库源代码 | 包 (NuGet) | Azure Developer CLI
先决条件
- Azure 开发人员 CLI
- Docker Desktop
- .NET SDK 9.0
如果没有 Azure 帐户,请在开始前创建一个免费帐户。
初始化项目
使用 Azure Developer CLI (azd
) 创建 Azure Cosmos DB for Table 帐户并部署容器化示例应用程序。 示例应用程序使用客户端库来管理、创建、读取和查询示例数据。
在空目录中打开终端。
如果尚未经过身份验证,请使用
azd auth login
向 Azure Developer CLI 进行身份验证。 按照该工具指定的步骤,使用首选 Azure 凭据向 CLI 进行身份验证。azd auth login
使用
azd init
来初始化项目。azd init --template cosmos-db-mongodb-dotnet-quickstart
在初始化期间,配置唯一的环境名称。
使用
azd up
部署 Azure Cosmos DB 帐户。 Bicep 模板还部署示例 Web 应用程序。azd up
在预配过程中,选择订阅、所需位置和目标资源组。 等待预配过程完成。 此过程可能需要大约 5 分钟。
预配 Azure 资源后,输出中将包含指向正在运行的 Web 应用程序的 URL。
Deploying services (azd deploy) (✓) Done: Deploying service web - Endpoint: <https://[container-app-sub-domain].azurecontainerapps.io> SUCCESS: Your application was provisioned and deployed to Azure in 5 minutes 0 seconds.
使用控制台中的 URL 在浏览器中导航到 Web 应用程序。 观察正在运行的应用的输出。
安装客户端库
客户端库可通过 NuGet 作为 MongoDB.Driver
包使用。
打开终端并导航到
/src/web
文件夹。cd ./src/web
使用
dotnet add package
安装MongoDB.Driver
包(如果尚未安装)。dotnet add package MongoDB.Driver
打开并查看 src/web/Microsoft.Samples.Cosmos.MongoDB.Quickstart.Web.csproj 文件以验证
MongoDB.Driver
条目是否存在。
对象模型
名称 | 描述 |
---|---|
MongoClient |
用于连接到 MongoDB 的类型。 |
Database |
表示帐户中的数据库。 |
Collection |
表示帐户中数据库内的集合。 |
代码示例
模板中的示例代码使用名为 cosmicworks
的数据库和名为 products
的集合。
products
集合包含每个产品的名称、类别、数量和唯一标识符等详细信息。 该集合使用 /category
属性作为分片键。
对客户端进行身份验证
此示例创建 MongoClient
类的新实例。
string connectionString = "<azure-cosmos-db-for-mongodb-connection-string>";
MongoClient client = new(connectionString);
获取数据库
此示例使用 MongoClient
类的 GetDatabase
方法创建 IMongoDatabase
接口的实例。
IMongoDatabase database = client.GetDatabase("<database-name>");
获取集合
此示例使用 IMongoDatabase
接口的 GetCollection<>
泛型方法创建泛型 IMongoCollection<>
接口的实例。 泛型接口和方法都使用另一类中定义的类型 Product
。
IMongoCollection<Product> collection = database.GetCollection<Product>("<collection-name>");
public record Product(
string id,
string category,
string name,
int quantity,
decimal price,
bool clearance
);
创建文档
使用 collection.ReplaceOneAsync<>
和泛型 Product
类型参数在集合中创建文档。 此方法会“更新插入”该项,有效地替换该项(如果该项已存在)。
Product document = new(
id: "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb",
category: "gear-surf-surfboards",
name: "Yamba Surfboard",
quantity: 12,
price: 850.00m,
clearance: false
);
await collection.ReplaceOneAsync<Product>(
d => d.id == document.id,
document,
new ReplaceOptions { IsUpsert = true }
);
读取文档
同时使用唯一标识符 (id
) 和分片键字段来执行点读取操作。 结合使用 collection.FindAsync<>
和 Product
类型参数以有效地检索特定项。
IAsyncCursor<Product> documents = await collection.FindAsync<Product>(
d => d.id == "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" && d.category == "gear-surf-surfboards"
);
Product? document = await documents.SingleOrDefaultAsync();
查询文档
使用 collection.AsQueryable()
和语言集成查询 (LINQ) 对容器中的多个项执行查询。 此查询查找指定类别(分片键)中的所有项。
IQueryable<Product> documents = collection.AsQueryable().Where(
d => d.category == "gear-surf-surfboards"
);
foreach (Product document in await documents.ToListAsync())
{
// Do something with each item
}
浏览数据
使用适用于 Azure Cosmos DB 的 Visual Studio Code 扩展来浏览 MongoDB 数据。 可以执行核心数据库操作,这些操作包括但不限于:
- 使用剪贴簿或查询编辑器执行查询
- 修改、更新、创建和删除文档
- 从其他源导入批量数据
- 管理数据库和集合
有关详细信息,请参阅如何使用 Visual Studio Code 扩展来浏览 Azure Cosmos DB for MongoDB 数据。
清理资源
不再需要示例应用程序或资源时,请删除相应的部署和所有资源。
azd down