首页 > 解决方案 > 在 WHERE 条件下使用日期时间过滤器进行 Cosmos db sql api 查询

问题描述

试图获取修改日期(日期时间)等于(当前日期 -7)但不工作的那些文档,伪代码如下

"SELECT * FROM c where c.LastModifiedDate = (GetCurrentDate-7)"

如何使用 cosmosdb sql api 做到这一点?没有 udf,因为我们有基于计时器触发器的 Azure 函数,该函数在每次触发时直接使用上述SQL 查询查询 Cosmos DB,并将结果绑定到函数的参数。

    [FunctionName("TimerCosmosDbWriteExample")]
    public static async Task Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer,
        TraceWriter log,
        [CosmosDB(
            databaseName: "Database",
            collectionName: "Collection",
            ConnectionStringSetting = "MyConnectionString",
            SqlQuery = "SELECT * FROM c where c.LastModifiedDate = GetCurrentDate-7")] IEnumerable<Entity> documents)
    {
     //function body -- loop through retrieved documents & process it 
    }

标签: azure-cosmosdbazure-cosmosdb-sqlapi

解决方案


我有2个观点供你参考:

1.由于在azure函数中调用sql查询,所以可以currentDate-7用代码计算,然后将其作为参数传递给查询sql。这是一种常见的用法,只需匹配LastModifiedDate列的格式。

2.我不知道你为什么说你不能在azure函数中使用udf和sql查询,这没有意义。据我所知,UDF是SQL的一部分。请参考我之前的案例:Query a specific time range data from cosmos db and store it in sql database via azure data factory


推荐阅读