快速入門:搭配Node.js使用適用於 MongoDB 的 Azure Cosmos DB (RU)
在本快速入門中,您會使用 Python 部署適用於 MongoDB 的基本 Azure Cosmos DB 應用程式。 適用於 MongoDB 的 Azure Cosmos DB 是無架構資料存放區,可讓應用程式使用 MongoDB 連結庫將非結構化檔案儲存在雲端中。 您將瞭解如何使用 Python 在 Azure Cosmos DB 資源內建立文件並執行基本工作。
連結庫原始程式碼 | 套件 (npm) | Azure 開發人員 CLI
必要條件
- Azure Developer CLI
- Docker Desktop
- Node.js 22 或更新
如果您沒有 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-nodejs-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 應用程式。 觀察執行中應用程式的輸出。
安裝用戶端程式庫
用戶端連結庫可透過 npm 取得,作為 mongodb
套件。
開啟終端機,然後導覽至
/src/ts
資料夾。cd ./src/ts
如果尚未安裝,則請使用
npm install
來安裝mongodb
套件。npm install --save mongodb
開啟並檢閱 src/ts/package.json 檔案,以驗證
mongodb
專案是否存在。
開啟終端機,然後導覽至
/src/js
資料夾。cd ./src/js
如果尚未安裝,則請使用
npm install
來安裝mongodb
套件。npm install --save mongodb
開啟並檢閱 src/js/package.json 檔案,以驗證
mongodb
專案是否存在。
物件模型
名稱 | 描述 |
---|---|
MongoClient |
用來連線到 MongoDB 的類型。 |
Database |
表示帳戶中的資料庫。 |
Collection |
表示帳戶中資料庫內的集合。 |
程式碼範例
範本中的範例程式代碼會使用名為 cosmicworks
的資料庫和名為 products
的集合。 集合 products
包含每個產品的名稱、類別、數量和唯一標識碼等詳細數據。 集合會 /category
使用 屬性作為分區索引鍵。
驗證用戶端
此範例會建立 型別 MongoClient
的新實例。
const connectionString = "<azure-cosmos-db-for-mongodb-connection-string>";
const client = new MongoClient(connectionString);
const connectionString = "<azure-cosmos-db-for-mongodb-connection-string>";
const client = new MongoClient(connectionString);
取得資料庫
此範例會使用 db
型別的 Db
函式,建立 型別的MongoClient
實例。
const database: Db = client.db("<database-name>");
const database = client.db("<database-name>");
取得集合
此範例會使用 collection
型別的 Collection
函式,建立 型別的Db
實例。
此函式具有泛型參數,其使用 Product
介面中定義的型別。
const collection: Collection<Product> = database.collection<Product>("<collection-name>");
export interface Product {
_id: string;
category: string;
name: string;
quantity: number;
price: number;
clearance: boolean;
}
const collection = database.collection("<collection-name>");
建立文件
使用 collection.updateOne
在集合中建立檔。 此方法會有效地 "upserts" 可取代項目 (如果已存在) 的項目。
var document: Product = {
_id: 'aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb',
category: 'gear-surf-surfboards',
name: 'Yamba Surfboard',
quantity: 12,
price: 850.00,
clearance: false
};
var query: Filter<Product> = {
_id: 'aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb',
category: 'gear-surf-surfboards'
};
var payload: UpdateFilter<Product> = {
$set: document
};
var options: UpdateOptions = {
upsert: true
};
var response: UpdateResult<Product> = await collection.updateOne(query, payload, options);
var document = {
_id: 'aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb',
category: 'gear-surf-surfboards',
name: 'Yamba Surfboard',
quantity: 12,
price: 850.00,
clearance: false
};
const query = {
_id: 'aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb',
category: 'gear-surf-surfboards'
};
const payload = {
$set: document
};
const options = {
upsert: true,
new: true
};
var response = await collection.updateOne(query, payload, options);
讀取文件
使用唯一標識碼 (id
) 和分區索引鍵字段來執行點讀取作業。 使用 collection.findOne
有效率地擷取特定項目。
var query: Filter<Product> = {
_id: 'aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb',
category: 'gear-surf-surfboards'
};
var response: WithId<Product> | null = await collection.findOne(query);
var query = {
_id: 'aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb',
category: 'gear-surf-surfboards'
};
var response = await collection.findOne(query);
查詢文件
使用 collection.find
,以對容器中的多個項目執行查詢。 此查詢會尋找指定類別內的所有專案(分區索引鍵)。
var query: Filter<Product> = {
category: 'gear-surf-surfboards'
};
var response: FindCursor<WithId<Product>> = await collection.find(query);
for await (const item of response) {
// Do something with each item
}
var query = {
category: 'gear-surf-surfboards'
};
var response = await collection.find(query);
for await (const item of response) {
// Do something with each item
}
探索資料
使用適用於 Azure Cosmos DB 的 Visual Studio Code 擴充功能來探索 MongoDB 數據。 您可以執行核心資料庫作業,包括但不限於:
- 使用剪貼簿或查詢編輯器執行查詢
- 修改、更新、建立和刪除檔
- 從其他來源匯入大量數據
- 管理資料庫和集合
如需詳細資訊,請參閱 如何使用Visual Studio Code擴充功能來探索適用於 MongoDB 的 Azure Cosmos DB 數據。
清除資源
當您不再需要範例應用程式或資源時,請移除對應的部署和所有資源。
azd down