使用spring-data-mongodb 2.0.9中的
Document document = new Document(); document.append("createTime",1); IndexOptions indexOptions = new IndexOptions(); indexOptions.expireAfter(300L,TimeUnit.SECONDS); this.mongoTemplate.getCollection("test").createIndex(document,indexOptions);
createTime是时间字段
使用spring-data-mongodb 1.7.0中的
BasicDBObject bson = new BasicDBObject(); bson.append("createTime",1); BasicDBObject options = new BasicDBObject(); options.append("expireAfterSeconds",7200); this.mongoTemplate.getCollection("test").createIndex(bson,options);