【问题标题】:Setting expiry time for a collection in mongodb using mongoose使用 mongoose 在 mongodb 中设置集合的到期时间
【发布时间】:2013-01-13 20:52:17
【问题描述】:

以下是可通过 mongo 终端设置集合到期时间(TTL)的命令:

db.log.events.ensureIndex( { "status": 1 }, { expireAfterSeconds: 3600 } )

如何使用 mongoose 从 Node.js 中的代码执行此操作?

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    在 Mongoose 中,您可以通过该字段的架构定义中的 expires 属性在 Date 字段上创建一个 TTL 索引:

    // expire docs 3600 seconds after createdAt
    new Schema({ createdAt: { type: Date, expires: 3600 }});
    

    注意:

    • MongoDB 的数据过期任务每分钟运行一次,因此过期的文档可能会在过期后持续一分钟。
    • 此功能需要 MongoDB 2.2 或更高版本。
    • 您可以在创建文档时将createdAt 设置为当前时间,或者按照here 的建议添加default 为您执行此操作。
      • { createdAt: { type: Date, expires: 3600, default: Date.now }}

    【讨论】:

    • updateLogSchema = mongoose.Schema({ success: { type: Boolean, required: true }, saveDate: { type: Date, required: true, default: Date.now, expires: 60*60*24*7 #Every 7 days } }) 所以我必须在一个日期属性上执行此操作?如果我更新我的文档和我的 saveDate 会发生什么,它还能再活 7 天吗?
    • @AndiGiga 您可能想查看 mongo 文档:docs.mongodb.org/manual/tutorial/expire-data
    • 如何在数组的特定元素中应用 ttl。我正在编写节点 API。我在字符串数组中保留令牌我想在指定的时间间隔后删除它们??
    • 那么如果我将expires设置为updatedAt,那么每次更新值后都会刷新计数?
    • 我正在遵循这种方法,但是无论我指出多少秒,它都会在一分钟后删除文档。 cartSchema.index({createdAt: 1},{expireAfterSeconds: 14400})
    【解决方案2】:

    这段代码对我有用。

    有帮助吗

    let currentSchema = mongoose.Schema({
        id: String,
        name: String,
        packageId: Number,
        age: Number
    }, {timestamps: true});
    
    currentSchema.index({createdAt: 1},{expireAfterSeconds: 3600});
    

    【讨论】:

    • 当你设置 {timestamps: true} 然后 createdAt 将被自动添加到字段列表中。
    • @SadeghTeimouri 哪个字段会过期?
    • 此集合中的每个文档,将在创建后 3600 秒后删除。
    • 真的吗?它对我不起作用,我遵循了版本
    【解决方案3】:

    有一个 npm 库 - 'mongoose-ttl'.:

    var schema = new Schema({..});
    schema.plugin(ttl, { ttl: 5000 });
    

    你可以看到这个库的所有选项: https://www.npmjs.com/package/mongoose-ttl

    【讨论】:

    • 这个库的最新版本是 8 年前发布的。似乎维护得不好...
    【解决方案4】:

    如果您不想处理过期时间计算并提高架构的整体可读性,则向 expires 提供字符串也可以很好地与 Mongoose 配合使用。

    例如,这里我们将expires 设置为2m(2 分钟),猫鼬会为我们转换为120 秒

    var TestSchema = new mongoose.Schema({
      name: String,
      createdAt: { type: Date, expires: '2m', default: Date.now }
    });
    

    Mongoose 会在后台创建一个索引并自动将 expireAfterSeconds 设置为在这种情况下为 120 秒(2m 指定)。

    重要的是要注意 TTL 进程 runs once every 60 seconds 所以它并不总是完全准时。

    【讨论】:

      【解决方案5】:

      如果您正在使用 Mongodb Atlas 副本集 - 请尝试:

      import * as mongoose from 'mongoose'; 
      
      let currentSchema = new mongoose.Schema({
              createdAt: { type: Date, expires: 10000, default: Date.now },
              id: String,
              name: String,
              packageId: Number,
              age: Number
              });
      
      currentSchema.index({"lastModifiedDate": 1 },{ expireAfterSeconds: 10000 });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-26
        • 2018-08-21
        • 1970-01-01
        • 1970-01-01
        • 2019-06-24
        • 2022-01-23
        相关资源
        最近更新 更多