在本快速入門中,您會使用 Python 部署適用於 MongoDB 的基本 Azure Cosmos DB 應用程式。 適用於 MongoDB 的 Azure Cosmos DB 是無架構資料存放區,可讓應用程式使用 MongoDB 連結庫將非結構化檔案儲存在雲端中。 您將瞭解如何使用 Python 在 Azure Cosmos DB 資源內建立文件並執行基本工作。
函式庫原始碼 | 套件(NuGet) | Azure 開發人員 CLI
必要條件
- Azure Developer CLI
- Docker 桌面
- .NET SDK 9.0
如果您沒有 Azure 帳戶,請在您開始之前先建立 免費帳戶。
初始化專案
使用 Azure 開發人員 CLI (azd) 建立適用於資料表帳戶的 Azure Cosmos DB,並部署容器化範例應用程式。 應用程式範例使用用戶端程式庫管理、建立、讀取和查詢樣本資料。
在空的目錄中開啟終端機。
如果您尚未通過驗證,請使用
azd auth login向 Azure 開發人員 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在布建程式期間,選取您的訂用帳戶、所需的位置和目標資源群組。 等候佈建程序完成。 此流程「大約需要五分鐘」的時間。
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如果尚未安裝,則請使用
MongoDB.Driver來安裝dotnet add package套件。dotnet add package MongoDB.Driver開啟並檢閱 src/web/Microsoft.Samples.Cosmos.MongoDB.Quickstart.Web.csproj 檔案,以驗證
MongoDB.Driver專案是否存在。
匯入程式庫
將 MongoDB.Driver 命名空間匯入您的應用程式程式代碼。
using MongoDB.Driver;
物件模型
| 名稱 | 描述 |
|---|---|
MongoClient |
用來連線到 MongoDB 的類型。 |
Database |
表示帳戶中的資料庫。 |
Collection |
代表帳戶中的資料庫所含的集合。 |
程式碼範例
範本中的範例程式代碼會使用名為 cosmicworks 的資料庫和名為 products的集合。 集合 products 包含每個產品的名稱、類別、數量和唯一標識碼等詳細數據。 集合會使用 /category 屬性做為分區索引鍵。
驗證用戶端
此範例會建立 類別的新實例 MongoClient 。
string connectionString = "<azure-cosmos-db-for-mongodb-connection-string>";
MongoClient client = new(connectionString);
取得資料庫
此範例使用 IMongoDatabase 類別的 GetDatabase 方法,建立 MongoClient 介面的一個實例。
IMongoDatabase database = client.GetDatabase("<database-name>");
取得集合
此範例會使用 IMongoCollection<> 介面的 GetCollection<> 泛型方法來建立泛型 IMongoDatabase 介面的執行個體。 泛型介面和方法都使用另一個類別中定義的類型 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 型別參數,在集合中建立檔。 此方法會有效地 "upserts" 可取代項目 (如果已存在) 的項目。
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)。 此查詢會尋找指定類別內的所有項目(Shard Key)。
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擴充功能來探索適用於 MongoDB 的 Azure Cosmos DB 數據。
清除資源
當您不再需要範例應用程式或資源時,請移除對應的部署和所有資源。
azd down --force --purge