Azure Cosmos DB输入结合 - 无法传递偏移,并将值限制为参数



我正在遇到一个问题,试图动态地将 OFFSETLIMIT的值作为查询参数传递给我的cosmosdb触发。

如果我将这两个值的值硬编码到查询中,则可以按预期工作。

但是,使用此代码:

 {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "route": "v1/query/properties",
      "methods": [
        "post"
      ]
    },
    {
      "name": "propertiesInquiryInput",
      "type": "cosmosDB",
      "databaseName": "property",
      "collectionName": "discovery",
      "connectionStringSetting": "CosmosDBConnectionString",
      "direction": "in",
      "leaseCollectionName": "leases",
      "sqlQuery": "SELECT * FROM c WHERE c.country={country} OFFSET {pageNo} LIMIT {perPage}"
    },

我在执行后会遇到以下失败:

System.Private.CoreLib: Exception while executing function: Functions.queryProperties. Microsoft.Azure.DocumentDB.Core: Message: {"errors":[{"severity":"Error","location":
{"start":48,"end":55},"code":"SC2062","message":"The OFFSET count value exceeds the maximum allowed value."},
{"severity":"Error","location":{"start":62,"end":70},"code":"SC2061","message":"The LIMIT count value exceeds the maximum allowed value."}]}

我对Azure服务及其模式相对较新,所以也许我在这里缺少一些明显的东西。我尝试通过帖子将这些值作为JSON对象发送,并通过Get请求作为查询参数。似乎没有任何作用。

我也不知道一种方法可以查看被触发的SQL查询,以便从该角度调试它。任何朝着正确方向的指导都将不胜感激。

更新:

为清晰添加功能主体:

module.exports = async function (context, req) {
    const results = context.bindings.propertiesInquiryInput;
    !results.length && context.done(null, { status: 404, body: "[]", });
    const body = JSON.stringify(results.map(data => reshapeResponse(data));
    return context.done(null, { status: 200, body });
}

除了原始问题:

在Python中使用参数性查询和azure.cosmos=4.2.0 PYPI软件包获得了相同的异常消息。

复制的情况:

query = f"""
SELECT *
FROM c
WHERE ...
AND c.run.submittedBy = @author
OFFSET 0 LIMIT @exp_limit
"""
items = list(cdb_container.query_items(
    query=query,
    parameters=[
        {"name":"@exp_limit", "value": f"{num_of_experiments}"}, # can't pass as param due to `The LIMIT count value exceeds the maximum allowed value.`
        {"name":"@author", "value": f"{submitter}"},
    ],
    enable_cross_partition_query=True
))

解决方案(查询格式的字符串(:

num_of_experiments = 10
query = f"""
SELECT *
FROM c
WHERE ...
AND c.run.submittedBy = @author
OFFSET 0 LIMIT {num_of_experiments}
"""

省略WHERE语句中的无关参数。可能对正在苦苦挣扎的人有帮助。

它有点晚了,但是我遇到了同样的问题,在我的情况下,由于我传递的值是一个字符串,因此出现了这个错误。在我将其解析为int之后,查询就像预期的一样工作!

相关内容

最新更新