如何在 CosmosDb 上为键值对数组的单个元素创建索引

How can I create index on CosmosDb for a single element of key-value pair array

我将 Azure CosmosDb 与 MongoDb Api 一起使用,并获得了具有以下结构的文档集合。 我必须按参数过滤文档,例如

x => x.Parameters.Any(xx => xx.Key == ParameterNames.ShiftId && (int)xx.Value == shiftId)

在我看来我需要创建索引以获得更好的性能,但我找不到任何关于如何创建索引的信息。

{
"_id": "08d8c696-2b7b-f227-d5dd-0647a8d51c1c",
"State": 2,
"Created": {
    "$date": "2021-02-01T09:45:54.986Z"
},
"TailId": "e8fb236e-4d48-417b-bf5a-73f1d48fe239",
"Parameters": [{
    "k": "ShiftId",
    "v": 181
}, {
    "k": "Id",
    "v": "147814878155"
}, ....
]

}

对于 Cosmos DB Mongo API,您可以尝试在 Parameters 上添加通配符索引,如 the docs.

中所述

如果您能够更改您的模型,您可能会通过将参数数组重构为更接近文档根的属性来获得更好的性能。例如:

{
  "_id": "08d8c696-2b7b-f227-d5dd-0647a8d51c1c",
  "State": 2,
  ...
  "ShiftId": 181,
}

{
  "_id": "08d8c696-2b7b-f227-d5dd-0647a8d51c1c",
  "State": 2,
  ...
  Parameters: {
    "ShiftId": 181,
    "Id": "147814878155" 
  }
}