基于时间戳查询 CosmosDB

Querying CosmosDB based on timestamps

我正在使用我的一位同事设置的 CosmosDB,并使用连接字符串连接到它。数据库包含多个 JSON 个具有以下架构的文档:

{
    "period": "Raw",
    "source": "Traffic",
    "batchId": "ee737270-0b72-49b7-a2f1-201f642e9c81",
    "periodName": "Raw",
    "sourceName": "Traffic",
    "groupKey": "gc4151_a",
    "partitionKey": "traffic-gc4151_a-raw-raw",
    "time": "2021-08-05T23:55:10",
    "minute": 55,
    "hour": 23,
    "day": 05,
    "month": 08,
    "quarter": 3,
    "year": 2021,
    "minEventTime": "2021-08-05T23:55:09",
    "maxEventTime": "2021-08-05T23:55:11",
    "meta": {
        "siteId": "GC4151_A",
        "from": {
            "lat": "55.860894822588506",
            "long": "-4.284365958508686"
        },
        "to": {
            "lat": "55.86038667864348",
            "long": "-4.2826901232101795"
        }
    },
    "measurements": {
        "flow": [
            {
                "calculation": "Raw",
                "name": "flow",
                "calculationName": "Raw",
                "value": 0
            }
        ],
        "concentration": [
            {
                "calculation": "Raw",
                "name": "concentration",
                "calculationName": "Raw",
                "value": 0
            }
        ]
    },
    "added": "2021-08-05T12:21:32.000819Z",
    "updated": "2021-08-05T12:21:32.000819Z",
    "id": "d4346f50-543e-4c4d-82cf-835b480914c2",
    "_rid": "4RRTAIYVA1AIAAAAAAAAAA==",
    "_self": "dbs/4RRTAA==/colls/4RRTAIYVA1A=/docs/4RRTAIYVA1AIAAAAAAAAAA==/",
    "_etag": "\"1c0015a1-0000-1100-0000-5f3fbc4c0000\"",
    "_attachments": "attachments/",
    "_ts": 1598012492
}

我正在尝试对 select 当前日期时间和一周前的所有记录编写 SQL 查询,以便我可以使用这些来执行未来的计算。

我尝试使用以下两种方法:

SELECT *
FROM c
WHERE c.time > date_sub(now(), interval 1 week);

SELECT *
FROM c
WHERE c.time >= DATE_ADD(CURDATE(), INTERVAL -7 DAY);

但是,这两个return出现如下错误:

Gateway Failed to Retrieve Query Plan: Message: {"errors":[{"severity":"Error","location":{"start":124,"end":125},"code":"SC1001","message":"Syntax error, incorrect syntax near '1'."}]}
ActivityId: 51c3b6f7-e760-4062-bd80-8cc9f8de5352, Microsoft.Azure.Documents.Common/2.14.0, Microsoft.Azure.Documents.Common/2.14.0

我的问题是我的代码有什么问题,我该如何解决?

您可以使用 DateTimeAdd and GetCurrentDateTime() 来实现这一点。例如

SELECT *
FROM c
WHERE c.time > DateTimeAdd("day",-7,GetCurrentDateTime() )

让我知道这是否适合你。