You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure CosmosClient中旧DocumentCollection索引策略的等价实现

Azure Cosmos DB 旧DocumentCollection到新ContainerProperties的等价写法

以下是你提供的旧版DocumentCollection代码在新Azure.Cosmos客户端中的等价实现:

using Azure.Cosmos;
using Azure.Cosmos.Indexes;

var collectionInfo = new ContainerProperties(
    id: this.SourceMapping.ResourceDescription.Name,
    partitionKeyPath: "/yourPartitionKeyPath" // 新版API创建容器必须指定分区键路径,需替换为实际业务路径
)
{
    IndexingPolicy = new IndexingPolicy(
        Index.RangeIndex.CreateStringIndex(precision: -1)
    )
};

关键升级说明

  • 类替换:旧版DocumentCollection直接替换为新版ContainerProperties
  • 分区键强制要求:新API创建容器时必须指定分区键路径,旧版允许的无分区键容器场景,升级时需补充对应配置
  • 索引策略构造逻辑:新API通过Index.RangeIndex.CreateStringIndex静态方法创建字符串范围索引,precision: -1保持旧版全精度索引的行为一致
  • 命名空间调整:需引用Azure.Cosmos和Azure.Cosmos.Indexes命名空间,替代旧版Microsoft.Azure.Documents相关命名空间

内容的提问来源于stack exchange,提问作者shelholmes221

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 01:22:03