azure - 在Azure函数的DocumentDB属性中发送SqlQuery

标签 azure azure-cosmosdb azure-functions

我有一个 Azure 函数,它使用 DocumentDB 属性连接到 Cosmos DB。我正在使用 Azure Functions for Visual Studio 2017 工具。这是简单的函数

    [FunctionName("DocumentDbGet")]
    public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Function, "get")]HttpRequestMessage req, TraceWriter log,
        [DocumentDB("FunctionJunctionDemo", "Demo")]IEnumerable<DemoModel> items)
    {
        //Can perform operations on the "items" variable, which will be the result of the table/collection you specify
        string name = req.GetQueryNameValuePairs()
            .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
            .Value;

        var item = items.Where(x => x.FirstName == name);
        return req.CreateResponse(HttpStatusCode.OK);
    }

我希望能够将 SqlQuery 作为 DocumentDB 属性的参数之一传递,如下所示:

    [FunctionName("DocumentDbGet")]
    public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Function, "get")]HttpRequestMessage req, TraceWriter log,
        [DocumentDB("FunctionJunctionDemo", "Demo", SqlQuery = $"select * from c where c.Id = {Id}")]IEnumerable<DemoModel> items)
    {
        //Can perform operations on the "items" variable, which will be the result of the table/collection you specify
        string name = req.GetQueryNameValuePairs()
            .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
            .Value;

        var item = items.Where(x => x.FirstName == name);
        return req.CreateResponse(HttpStatusCode.OK);
    }

我只见过 1 个示例这样做,并报告说它应该有效。 https://github.com/Azure/Azure-Functions/issues/271 我收到的“错误”是它无法将任何名为 SqlQuery 的内容识别为可能的参数。

我查看了 Azure 函数输入绑定(bind)的文档 https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-documentdb#input-sample-with-multiple-documents它显示了 function.json 文件中包含 sqlQuery 属性的输出。它是怎么进来的?

如果无法在 DocumentDB 属性中传递 SqlQuery,那么预先过滤结果以避免返回整个集合然后通过LINQ 查询?

最佳答案

您需要引用 Microsoft.Azure.WebJobs.Extensions.DocumentDB NuGet 包的 1.1.0-beta 版本(或更高版本)。

在该版本中,SqlQueryDocumentDB 属性的有效参数。如果我在 select 字符串之前删除 $ 符号,您的代码就会为我编译:

[DocumentDB("FunctionJunctionDemo", "Demo", SqlQuery = "select * from c where c.Id = {Id}")]

您不需要 $ - 它用于 C# 中的字符串插值,而不是您想要在此处执行的操作。

关于azure - 在Azure函数的DocumentDB属性中发送SqlQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45471234/

相关文章:

c# - 在 azure blob 存储中就地创建 zip 文件

python - Azure Python 函数应用程序中的 HTTP 请求

azure - 无法使用 Mongo 连接到运行 Docker 的 Azure Cosmos DB 模拟器

azure - 如何在azure功能中浏览或下载/home的内容?

azure - 使用 Azure 功能的命令需要哪些 Azure 角色/权限

c# - 如何关闭 Microsoft Azure Visual Studio C# WebRole WorkerRole 调试输出噪音?

.net - 防伪 cookie token 和表单字段 token 不匹配 - Orchard 1.8.1

azure-cosmosdb - 使用 Gremlin 进行地理位置搜索

Azure Cosmos DB 模拟器 : Unauthorized after upgrate to 2. 9.2

使用 Function App 进行 Azure API 管理