通过重置TTL

时间:2018-06-19 13:33:44

标签: mongodb meteor

我正在尝试创建一个持续120秒的文档,一旦我调用此方法,我就希望TTL重新启动。

目前,我无法更新我的文档,在120 sc ..之后,该文档将被删除并重新创建,而不是总是更新。

有我的收藏集:

LaptopConnections = new Mongo.Collection('laptopConnection');

let LaptopConnectionSchema = new SimpleSchema({
  creationDate: {
    type: Date,
    label: "Date when the laptop was created",
    defaultValue: Date.now
  },

  "state.date": {
    type: Date,
    label: "time when the laptop was updated",
    autoValue: function () {
      return new Date;
    }
  }
}
  , { timestamps: true }
)
LaptopConnections.attachSchema(LaptopConnectionSchema)

这是我的方法:

Meteor.startup(() => {
    LapConnections._ensureIndex({ creationDate: 1 }, { expireAfterSeconds: 120 });// Will delete the collection after ~~ two minutes,

});
Meteor.methods({

  create_lapconnection(lap_id) {
    check(lap_id, String);
    if (!LapConnections.findOne({ _id: lap_id })) {
      console.log('workiiing',lap_id)
      LaptopConnections.insert({
        _id: box_id,
        creationDate: Date.now(),
      });
    } else {
      console.log('updated',lap_id)
      LaptopConnections.update({ _id: lap_id }, { upsert: true }, {
        $set: {
          "state.date": Date.now(),
        }
      }
      );

    }
  }
}) 

1 个答案:

答案 0 :(得分:0)

您正在更新state.date字段,而索引位于creationDate字段上。更新creationDate或将索引更改为使用state.date字段后,它应该可以工作。