Azure cosmosDB 不会删除 TTL 已过期的文档

Azure cosmosDB does not delete documents with expired TTL

我在 Azure cosmosDB 中有一个集合,我在其中启用了没有默认值的生存时间 (TTL) 功能。

根据我将在这个问题中提到的 documentation,一切似乎 crystal 清楚,但我仍然缺少一些东西,因为 5 天后我仍然可以看到文档300 秒的 TTL。

在显示收集设置的屏幕截图下方,其中 TTL 已启用且没有默认设置。

根据文档:

Azure Cosmos DB will automatically remove these items after the time period, since the time they were last modified. Time to live value is configured in seconds. When you configure TTL, the system will automatically delete the expired items based on the TTL value, without needing a delete operation that is explicitly issued by the client application.

但这似乎不适用于我的情况,在 17/07/2019 我可以使用 300 秒(5 分钟)的 TTL 看到超过 5 天的文档。

{
    "ClientEnqueuedUtcTime": "2019-07-12T06:49:53.844",
    "ClientDispatchedUtcTime": "2019-07-12T06:49:53.844",
    "ServerEnqueuedUtcTime": "2019-07-12T06:49:53.8949771",
    "ServerDispatchedUtcTime": "2019-07-12T06:49:54.3659741",
    "TTL": 300,
    "EventProcessedUtcTime": "2019-07-12T06:49:55.3583521Z",
    "PartitionId": 2,
    "EventEnqueuedUtcTime": "2019-07-12T06:49:55.25Z",
    "id": "4a0edf24-6a86-4a59-f55d-d7dfe47c30fa",
    "_rid": "SBk4AJadUE6gAgEAAAAAAA==",
    "_self": "dbs/SBk4AA==/colls/SBk4AJadUE4=/docs/SBk4AJadUE6gAgEAAAAAAA==/",
    "_etag": "\"00008f1d-0000-0200-0000-5d282d930000\"",
    "_attachments": "attachments/",
    "_ts": 1562914195
}

回答后更新

我尝试将 TTL 保存为小写格式,但它被自动转换为大写,所以我认为这与预期的 TTL 密钥大小写不匹配。

我应该做或尝试其他事情吗?

解决方案:

ttl确实是区分大小写的,你可以把它设置成小写,在我做的测试中出于某种原因,ttl仍然是大写的,而我期望它是小写的, 所以我错误地认为这是某种保留密钥。

如您所见here 属性 的情况导致它不起作用。

需要小写。我刚刚用 TTLttl 进行了测试,实际上第一个不起作用,但第二个确实如文档中所述起作用。可能是您的 Json 序列化程序迫使它变为大写。

您可以使用 JsonProperty 属性将 属性 的大小写强制为小写。这是 Microsoft 文档中 ttl 属性 的示例。


    // Include a property that serializes to "ttl" in JSON
    public class SalesOrder
    {
        [JsonProperty(PropertyName = "id")]
        public string Id { get; set; }
        [JsonProperty(PropertyName="cid")]
        public string CustomerId { get; set; }
        // used to set expiration policy
        [JsonProperty(PropertyName = "ttl", NullValueHandling = NullValueHandling.Ignore)]
        public int? ttl { get; set; }

        //...
    }