共用方式為


在 Azure Cosmos DB for MongoDB 中建立集合

適用於: MongoDB

此文章描述在 Azure Cosmos DB for MongoDB 中建立集合的各種方式。 它示範如何使用 Azure 入口網站、Azure CLI、PowerShell 或支援的軟體開發工具包 (SDK) 來建立集合。 本文將示範如何建立集合、指定分割區索引鍵,以及佈建輸送量。

附註

[容器] 和 [集合] 類似於關聯式資料庫中的資料表。 我們在 Azure Cosmos DB for NoSQL 以及整個 Azure 入口網站中都會使用容器一詞,但是會在 Azure Cosmos DB for MongoDB 的內容中使用集合一詞,以符合 MongoDB 中所使用的術語。

此文章描述在 Azure Cosmos DB for MongoDB 中建立集合的各種方式。 如果您使用不同的 API,請參閱 API for NoSQLAPI for CassandraAPI for GremlinAPI for Table 等文章來建立集合。

附註

建立集合時,請確定您不會建立兩個名稱相同但大小寫不同的集合。 那是因為 Azure 平台的某些部分區分大小寫不嚴格,這可能會導致與這類名稱相關的遙測數據和集合操作發生混淆或衝突。

使用 Azure 入口網站建立

  1. 登入 Azure 入口網站

  2. 建立新的 Azure Cosmos DB 帳戶,或選取現有的帳戶。

  3. 開啟 [資料總管] 窗格,然後選取 [新增容器]。 接下來,提供下列詳細資料:

    • 指出您要建立新的資料庫或使用現有的資料庫。
    • 輸入容器識別碼。
    • 輸入分區索引鍵。
    • 輸入要佈建的輸送量 (例如 1000 RU)。
    • 選擇 確定

    適用於 MongoDB 的 Azure Cosmos DB 螢幕快照,新增容器對話框

使用 .NET SDK 建立

var bson = new BsonDocument
{
    { "customAction", "CreateCollection" },
    { "collection", "<CollectionName>" },//update CollectionName
    { "shardKey", "<ShardKeyName>" }, //update ShardKey
    { "offerThroughput", 400} //update Throughput
};
var shellCommand = new BsonDocumentCommand<BsonDocument>(bson);
// Create a collection with a partition key by using Mongo Driver:
db.RunCommand(shellCommand);

如果您在建立集合時遇到逾時例外狀況,請執行讀取作業來驗證是否已成功建立集合。 在集合建立作業成功之前,讀取操作會擲回例外狀況。 如需有關建立作業支援的狀態碼清單,請參閱 Azure Cosmos DB 的 HTTP 狀態碼一文。

使用 Azure Resource Manager 範本建立集合

使用 Resource Manager 範本建立適用於 API for MongoDB 的 Azure Cosmos DB 集合

下一步