【问题标题】:SqlQuery in Azure Function ARRAY_CONTAINS not workAzure 函数 ARRAY_CONTAINS 中的 SqlQuery 不起作用
【发布时间】:2019-01-03 16:00:31
【问题描述】:

我在 cosmo db 中定义了一个函数,定义如下。在编辑器中查询有效,但是当我运行该函数时,它返回 500 Internal Server Error

我的文档:

{
    "user": "428",
    "year": "2019",
    "id": "1",
    "dataType": "LineString",
    "dataCategory": "realPath",
    "tripNumber": "A02232",
    "currentCoordinate": [
        13.845224,
        43.02356
    ],
    "deliveries": [
        {
            "devNumber": "001",
            "unloadSeq": "1",
            "currentDev": "1",
            "email": "punto1@mail.it",
            "targetCoordinate": [
                13.965224,
                43.95356
            ],
            "coordinates": [
                [
                    13.790663,
                    43.028926
                ],
                [
                    13.791447,
                    43.029169
                ],
                [
                    13.792198,
                    43.029561
                ],
                [
                    13.793775,
                    43.030549
                ],
                [
                    13.794601,
                    43.0312
                ],
                [
                    13.795577,
                    43.031835
                ],
                [
                    13.797047,
                    43.032737
                ],
                [
                    13.797605,
                    43.033153
                ],
                [
                    13.798249,
                    43.033647
                ],
                [
                    13.798732,
                    43.03367
                ],
                [
                    13.800126,
                    43.033678
                ],
                [
                    13.801661,
                    43.033725
                ],
                [
                    13.802755,
                    43.034172
                ],
                [
                    13.845224,
                    43.02356
                ]
            ]
        },
        {
            "devNumber": "008",
            "unloadSeq": "2",
            "currentDev": "0",
            "email": "punto2@mail.it",
            "targetCoordinate": [
                13.995224,
                43.99356
            ],
            "coordinates": []
        }
    ],
    "_rid": "3pRjAIHZRNUBAAAAAAAAAA==",
    "_self": "dbs/3pRjAA==/colls/3pRjAIHZRNU=/docs/3pRjAIHZRNUBAAAAAAAAAA==/",
    "_etag": "\"00006b08-0000-0000-0000-5c2def460000\"",
    "_attachments": "attachments/",
    "_ts": 1546514246
}

在编辑器中查询就可以了:

SELECT * FROM Trip c where ARRAY_CONTAINS(c.deliveries, {"currentDev": "1", "email": "punto1@mail.it"}, true)

function.json 中的查询不起作用:

{
  "bindings": [
    {
      "authLevel": "function",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "methods": [
        "get",
        "post"
      ],
      "route": "getRoutes/{emailpar}"
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    },
    {
      "type": "cosmosDB",
      "name": "inDocuments",
      "databaseName": "cdb-01",
      "collectionName": "myCollection",
      "connectionStringSetting": "mpn_COSMOSDB",
      "direction": "in",
      "sqlQuery": "SELECT * FROM Trip c where ARRAY_CONTAINS(c.deliveries, {currentDev: '1', email: {emailpar}}, true)"
    }
  ]
}

我认为通过方括号{}在数组中定义要搜索的参数和对象是语法问题。

【问题讨论】:

  • 你的分区键属性是什么?
  • 有两件事可能是错误的。首先,我建议在电子邮件属性周围添加单引号,这样很多查询看起来像这样:SELECT * FROM Trip c where ARRAY_CONTAINS(c.deliveries, {currentDev: '1', email: '{emailpar}'}, true)
  • 其次,您可能需要转义不引用字符串插值属性的大括号。您可以可能通过添加一个额外的花括号来做到这一点,使字符串看起来像这样:SELECT * FROM Trip c where ARRAY_CONTAINS(c.deliveries, {{currentDev: '1', email: '{emailpar}'}}, true)
  • 感谢@NickChapsas 它有效:SELECT * FROM Trip c where ARRAY_CONTAINS(c.deliveries, {{currentDev: '1', email: {emailpar}}}, true) 我的分区键是 id/user,但并不完美。我还在琢磨怎么选,这样你就没有热键了
  • 太棒了。我写了答案。请接受。

标签: azure-functions azure-cosmosdb


【解决方案1】:

需要对sql查询字符串的非参数字符串插值对象进行转义。您可以通过在每个引用中添加一个额外的花括号来做到这一点。

这意味着你的查询字符串必须是:

SELECT * FROM Trip c where ARRAY_CONTAINS(c.deliveries, {{currentDev: '1', email: {emailpar}}}, true)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-07
    • 2021-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    相关资源
    最近更新 更多