CosmosDB MongoDB API是否支持expireAfterSeconds的值为-1以表示”无限”的TTL?

huangapple go评论55阅读模式
英文:

Does CosmosDB MongoDB API support expireAfterSeconds of -1 for "Infinity" TTL?

问题

我有一个收集,其中不同类型的文件具有不同的有用寿命。实体的主要文件不应自动删除,但它可以具有在固定时间后根据策略变老的附属关联文件。

为此,我正在查看每个文件的TTL。

文档中的“为文件设置生存时间值”部分说明

也支持每个文件的TTL值。文档必须包含根级属性“ttl”(小写),并且必须为该收集创建了如上所述的TTL索引。在文件上设置的TTL值将覆盖收集的TTL值。

由于我必须创建收集级别的TTL索引,我是否必须将expireAfterSeconds设置为某个较高的数字(如2147483647秒),还是将此值设置为-1与使用NoSQL API 作为“无限”的容器级TTL方式相同?

关于“使用Azure Cosmos DB的MongoDB API过期数据”的文档页面的其余部分没有提到-1作为一个潜在值。

英文:

I have a collection where different types of documents have different useful life spans. The main document for an entity should never be automatically deleted but it can have ancillary associated documents which are viable to age out by policy after a fixed period.

To that end I am looking at per document TTL.

The "Set time to live value for a document" section of the documentation states

> Per-document TTL values are also supported. The document(s) must
> contain a root-level property "ttl" (lower-case), and a TTL index as
> described above must have been created for that collection
. TTL values
> set on a document will override the collection's TTL value.

As it is mandatory for me to create a collection level ttl index do I have to set expireAfterSeconds to some high number (such as 2147483647 seconds) or will setting this value to -1 work the same way as setting the container level ttl with the NoSQL API to act as "infinite"?

The remainder of the documentation on the "Expire data with Azure Cosmos DB's API for MongoDB" page does not mention -1 as a potential value.

答案1

得分: 1

Time-to-live (ttl) 在 MongoDB API 中遵循与核心 SQL API 相同的规则(甚至在后台使用 _ts 属性进行操作)。

您确实可以将 ttl 设置为 -1,作为集合级别的默认值(表示... 不过期),然后在特定文档上覆盖 ttl。像这样的内容应该可以工作,以在默认情况下启用集合的不删除 ttl:

db.coll.createIndex( {"_ts":1}, {expireAfterSeconds: -1} )

然后,您可以在每个文档基础上进行覆盖:

db.mycollection.insert( {someProperty: "some value", ttl: 3600 })

供参考:此文档 描述了 MongoDB API 中有关 ttl 的规则,其中明确提到其行为与 SQL API 相同:

Azure Cosmos DB 的 MongoDB API 中的 TTL 索引和每个文档的 TTL 值的逻辑与 SQL API 中的相同。

英文:

Time-to-live (ttl) with MongoDB API follows the same rules as the core SQL API (it even utilizes the _ts property in the background, for its operation).

You can indeed set ttl to -1 as the collection-level default (meaning... don't expire), and then override with ttl on specific documents. Something like this should work, to enable ttl on a collection with no deletion, by default:

db.coll.createIndex( {"_ts":1}, {expireAfterSeconds: -1} )

Then you can override, on a per-document basis:

db.mycollection.insert( {someProperty: "some value", ttl: 3600 })

For reference: this doc describes the MongoDB API rules for ttl, including this explicit mention of behavior being the same as with the SQL API:

> The logic governing TTL indexes and per-document TTL values in Azure Cosmos DB's API for MongoDB is the same as in Azure Cosmos DB.

huangapple
  • 本文由 发表于 2023年3月1日 08:16:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75598516.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定