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

Azure Cosmos DB NoSQL仅索引/id属性的容器创建报错求助

解决Azure Cosmos DB NoSQL容器仅索引/id属性的BadRequest错误

问题出在索引路径的格式设置错误,以下是修正方案:

正确的索引政策配置逻辑

要实现仅对/id属性建立索引,需按以下规则配置:

  • 将IncludedPaths设置为明确的/id路径,指定只索引该属性
  • 将ExcludedPaths设置为/*,排除所有其他属性的索引
  • 注意:consistent模式下系统属性id和_ts会自动被索引,明确配置/id可确保符合预期需求

修正后的C#代码

Microsoft.Azure.Cosmos.ContainerProperties container_props;
Microsoft.Azure.Cosmos.IndexingPolicy indexing_policy;

// 设置容器属性
container_props = new();
container_props.Id = "mycontainer";
container_props.PartitionKeyPath = "/id";

// 设置索引政策
indexing_policy = new IndexingPolicy();
indexing_policy.IndexingMode = IndexingMode.Consistent;

// 仅包含/id路径
indexing_policy.IncludedPaths.Add(new IncludedPath { Path = "/id" });
// 排除所有其他路径
indexing_policy.ExcludedPaths.Add(new ExcludedPath { Path = "/*" });

container_props.IndexingPolicy = indexing_policy;

// 创建容器
await db.CreateContainerIfNotExistsAsync(container_props);

错误原因说明

你之前使用的"[]"或"\"[]\""都不是合法的索引路径格式:

  • 索引路径必须遵循JSON路径语法,单个属性用/属性名格式,通配符用/*
  • 官方文档中"排除所有属性路径但保持索引激活"的场景,是通过清空IncludedPaths并设置ExcludedPaths为/*实现(此时系统属性仍自动索引),但这和你需要仅索引/id的需求不同

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 03:26:06