简体   繁体   English

Azure function cosmos db sql 在字符串数组上查询数组

[英]Azure function cosmos db sql query with ARRAY CONTAINS on string array

I' using Azure Functions and Cosmos DB with javascript to make the following query:我使用 Azure 函数和 Cosmos DB 与 javascript 进行以下查询:

SELECT * FROM Posts p WHERE ARRAY_CONTAINS(p.audience, "username_1")

It works when I try it on the website but requires the quotes.当我在网站上尝试它时它可以工作,但需要引号。 However, in my function.json I have:然而,在我的function.json我有:

{
  "bindings": [
    {
      "authLevel": "anonymous",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "methods": [
        "get"
      ],
      "route": "getFeed/{username}"
    },
    {
      "name": "res",
      "type": "http",
      "direction": "out"
    },
    {
      "type": "cosmosDB",
      "name": "userItem",
      "databaseName": "TwitterClone",
      "collectionName": "Users",
      "connectionStringSetting": "CosmosDBConnection",
      "direction": "in",
      "sqlQuery" : "SELECT * FROM Posts p WHERE ARRAY_CONTAINS(p.audience, {username})"
    }
  ],
  "disabled": false

} }

And I just get no results.我只是没有得到任何结果。 I guess it is because of the lack of quotes around {username} ...So how do I add quotes or get over the issue?我想这是因为{username}周围没有引号......那么我该如何添加引号或解决这个问题?

Here is an example document:这是一个示例文档:

{
    "author_username": "username_2",
    "audience": [
        "username_1",
        "username_3"
    ],
}

Can you try this?你能试试这个吗?

SELECT * FROM posts p where ARRAY_CONTAINS(p.auience, {{username:{username}}}, true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以使用变量从 function 中动态调用 SQL 查询 Azure 函数的 Cosmos DB 输入绑定? - Can the SQL query for an Azure Function's Cosmos DB input binding be called dynamically, with variables, from within the function? 使用 array-contains 和 in (firebase 9) 查询 firestore DB - Query firestore DB with array-contains and in (firebase 9) 将 JSON 数据从 Azure SQL DB 迁移到 Cosmos DB 结果为字符串值 - Migrate JSON data from Azure SQL DB to Cosmos DB results in string values Cosmos DB UDF 将 Object 转换为 JavaScript 中的数组 - Cosmos DB UDF Convert an Object to an Array in JavaScript 使用JavaScript Azure函数写入Azure Cosmos DB - Write to Azure Cosmos DB using JavaScript Azure Function 如果字符串包含数组中的任何项,则调用函数 - Call a function if a string contains any items in an array 如何通过 Azure Cosmos DB 中的存储过程查询数据库 - How to Query Database through Stored Procedure in Azure Cosmos DB Azure cosmos DB仅在查询中最多允许5个联接 - Azure cosmos DB allows max 5 joins only in a query 如何确定字符串是否包含数组,对象数组或函数定义 - How to determine if string contains array, object array or function definition 天蓝色函数http触发器输入cosmos db文档 - azure function http trigger inputting cosmos db document
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM