【问题标题】:How to write a query in Mongo to remove records where DateTimeOffset is greater than 30 days如何在 Mongo 中编写查询以删除 DateTimeOffset 大于 30 天的记录
【发布时间】:2019-07-19 19:16:30
【问题描述】:

【问题讨论】:

    标签: mongodb datetimeoffset robo3t


    【解决方案1】:

    您可以在创建记录时设置 TTL 索引。TTL 索引是特殊的单字段索引,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/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多