我正在尝试通过sqlQuery在where子句中使用带float参数的JavaScript Azure Function数据绑定到Cosmos DB。
这是来自function.json的绑定定义:
无效,我相信不会返回任何结果,因为经纬度被视为字符串:
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"route": "GetLoq/{lat:float}/{lon:float}"
},
{
"type": "http",
"direction": "out",
"name": "res"
},
{
"type": "cosmosDB",
"name": "doc",
"databaseName": "%CosmosDBNAME%",
"collectionName": "%CosmosCollectionNAME%",
"sqlQuery": "SELECT * FROM c where c.location.coordinates = [{lat}, {lon}]",
"connectionStringSetting": "DB",
"direction": "in"
}
]
当我对值进行硬编码(以进行比较)时有效:
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"route": "GetLoq/{lat:float}/{lon:float}"
},
{
"type": "http",
"direction": "out",
"name": "res"
},
{
"type": "cosmosDB",
"name": "doc",
"databaseName": "%CosmosDBNAME%",
"collectionName": "%CosmosCollectionNAME%",
"sqlQuery": "SELECT * FROM c where c.location.coordinates = [36.71, 3.25]",
"connectionStringSetting": "DB",
"direction": "in"
}
]
答案 0 :(得分:1)
该查询不起作用,因为它搜索的是字符串值["36.71", "3.25"]
而不是浮点数。
有一个issue尚未解决。
路由约束允许在HttpTrigger路由属性上为查询字符串参数指定数据类型。这些约束仅用于匹配路线。 使用绑定参数时,数据类型将转换为字符串。
我们必须创建一个UDF toFloat
(单击集合旁边的更多选项图标> New UDF)以将字符串首先转换为浮点型。
function stringToFloatUDF(input){
return parseFloat(input);
}
然后使用UDF修改sqlQuery。
"sqlQuery": "SELECT * from c where c.location.coordinates = [udf.toFloat({lat}), udf.toFloat({lon})]",