我目前在尝试为我的 cosmosdb 容器之一定义索引策略时遇到问题。我在 cosmosdb 中有一个容器,用于保存与用户 session 相关的数据。我使用 Node sdk 来定义容器、分区键和索引策略。以下是我如何定义 session 容器的索引策略和分区键。
else if(containerId.includes("sessions"))
{
indexingPolicy = {
includedPaths: [ {"path" : "/startTime/"} ]
};
partitionKey = { paths: ["/id"] };
}
当我发出 POST 请求并且容器不存在时,它将根据我在配置文件中定义的策略创建容器。但是,当我尝试发布新 session 时,根据索引策略,我收到各种错误。这是我到目前为止所尝试过的以及我遇到的错误。
{"path" : "/startTime/"} --> The indexing path '\/startTime\/' could not be accepted, failed near position '11'. Please ensure that the path is a valid path. Common errors include invalid characters or absence of quotes around labels.
{"path" : "/startTime/*"} --> "The special mandatory indexing path \"\/\" is not provided in any of the path type sets. Please provide this path in one of the sets."
{"path" : "/startTime/?"} --> "The special mandatory indexing path \"\/\" is not provided in any of the path type sets. Please provide this path in one of the sets."
{"path" : "/startTime"} --> "The indexing path '\/startTime' could not be accepted, failed near position '10'. Please ensure that the path is a valid path. Common errors include invalid characters or absence of quotes around labels."
任何有关如何正确定义索引策略的帮助将不胜感激!
最佳答案
Any indexing policy has to include the root path /* as either an included or an excluded path
a path leading to a scalar value (string or number) ends with /?
为了让您的代码正常工作,您可以尝试以下操作:
const indexingPolicy = {
includedPaths: [ {"path" : "/*"},{"path" : "/startTime/?"} ]
}
await container.replace({
id: containerId,
partitionKey: { paths: ["/id"] },
indexingPolicy: indexingPolicy
});
但据我所知,如果您的includedPaths中有{"path": "/*"}
,则无需添加{"path": "/开始时间/?"}
。因为/*
可以包含"/startTime/?"
。
关于javascript - 如何使用 nodejs sdk 在 cosmos db 中定义正确的索引路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66371727/