azure - 如何使用 Mongo API 在共享吞吐量 cosmosdb 中创建集合

标签 azure azure-cosmosdb azure-cosmosdb-mongoapi

我有一个 Azure CosmosDb 数据库,其中 database-level throughput已提供。我们正在针对此 Cosmos 实例使用 MongoDB API。共享吞吐量模型要求所有集合都指定一个分区键,这似乎几乎阻止了除 Azure 门户或官方 Azure Cosmos SDK 之外的任何工具创建集合。例如,在 Robo 3T 中,尝试创建集合会导致以下错误:

Failed to create collection 'mycollection'.

Error: Shared throughput collection should have a partition key

尝试通过 mongoose(类似于 this question )或其他工具创建集合时,会发生相同的错误。

所以我想操作问题可以归结为: 有没有办法通过MongoDb API将所需的partitionKey传递给CosmosDb,以便集合创建成功?

最佳答案

通过 db.runCommand(...) 使用 shardCollection 命令

事实证明,有一种使用 MongoDb 有线协议(protocol)来实现这一目标的可行方法,尽管有些晦涩难懂。您可以使用 Cosmos 分区键(概念上映射到 Mongo 分片键)创建集合,方法是发出数据库级命令来为尚不存在的集合设置分片键:

在 mongo shell 中:

db.runCommand({shardCollection: "myDbName.nameOfCollectionToCreate", 
               key: {nameOfDesiredPartitionKey: "hashed"}})

运行此命令后,我的 ComosDb 数据库(具有数据库级共享吞吐量)现在包含新集合,并适当设置了分区键!

我还没有找到直接通过 Mongoose 调用 runCommand 的方法,但至少这种 native /有线协议(protocol)方法应该与任何官方 MongoDb 驱动程序一起使用,因此更加便携而不是仅仅依靠 Azure Cosmos SDK 来创建集合。

关于azure - 如何使用 Mongo API 在共享吞吐量 cosmosdb 中创建集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54522735/

相关文章:

Azure DocumentDB 单一帐户需要为多个集合付费吗?

python-2.7 - 如何使用 azure cosmos db 最大化数据库上传速率

azure - 使用 Mongodb 和逻辑应用程序插入文档时出现问题

azure-cosmosdb - Azure Cosmos Mongo API 版本 3.2 升级到 3.6

azure - 需要在 Azure 上执行哪些操作才能允许我们的 ASP.NET Core 6.0 MVC 根据 Azure AD 对用户进行身份验证

通过 Azure DevOps 成功进行 CI/CD 后,Azure FunctionApp 没有功能

azure - "Maximum request size of document and attachment"在 Azure DocumentDB 限制中意味着什么?

azure-cosmosdb - Azure cosmos db 在没有分区键的情况下速度更快

Azure 存储 Blob : what kind of blob do I get with GetBlobReference()?

使用 Terraform 的 Azure 容器注册表权限