如何在 Mongo 中编写查询以删除 DateTimeOffset 大于 30 天的记录

How to write a query in Mongo to remove records where DateTimeOffset is greater than 30 days

A capture from mongoDb with the structure of a Date time

您可以在创建时设置 TTL 索引 records.TTL 索引是特殊的 single-field 索引,MongoDB 可以使用它在特定时间后或在特定时间从集合中自动删除文档一个特定的时钟时间。

要创建 TTL 索引,请在值为日期或包含日期的数组的字段上使用带有 expireAfterSeconds 选项的 db.collection.createIndex() 方法值。

例如,要创建一个 TTL 索引,在 User 集合的 createdDate 字段上删除 30 天后的记录,请在 mongo shell 中使用以下操作:

db.User.createIndex( { "createdDate": 1 }, { expireAfterSeconds: 2592000 } )

src: https://docs.mongodb.com/manual/core/index-ttl/