javascript - 如何使用 nodejs sdk 在 cosmos db 中定义正确的索引路径?

标签 javascript node.js azure indexing azure-cosmosdb

我目前在尝试为我的 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/?"

引用号:Indexing policies in Azure Cosmos DB

关于javascript - 如何使用 nodejs sdk 在 cosmos db 中定义正确的索引路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66371727/

相关文章:

javascript - JavaScript如何让进度条缓慢移动

javascript - 如何将移动物体与背景居中?

node.js - 无法向 pdf 中的页眉/页脚添加背景颜色

javascript - 如何批量插入CouchDB文档?

node.js - 如何使用 ref 在 mongodb - mongoose 中正确存储文档?

git - 我可以使用 ssh key 进行 Azure git Push 部署吗?

rest - Power BI REST API : The remote server returned an error

javascript - 使用 javascript 从输入类型 url 获取并加载图像

c# - 将存档的 blob 复制到在线层

javascript - 调用与内部函数同名的外部函数