Я знаю, что с помощью Spring Data для MongoDB вы можете настроить удаление документа:
@Indexed(name = "ttl", expireAfterSeconds = 12)
private LocalDateTime expireAfterSeconds;
или
@Indexed(name = "ttl", expireAfter = "12s")
private LocalDateTime expireAfterSeconds;
Эти документы будут удалены по истечении времени, указанного в аннотации; оно будет одинаковым для каждого создаваемого документа.
Я бы хотел чтобы понять, можно ли еще создать индекс TTL, но значение можно рассчитать для каждого документа. (Например, в DynamoDB вы передаете время эпохи в секундах)
Что-то вроде
@Document("groceryitems")
public class GroceryItem {
@Id
private String id;
@Indexed(name = "deleteThisRecordAfter", expireAfter = "@checkActualValue")
private LocalDateTime expireAfter;
// or
@Indexed(name = "deleteThisRecordAfter", ttlIndex = true)
/*
* ttlIndex = true doesn't exist,
* but I would like to only have a TTL index and then provide a value for the field
* that mongod will use to determine when to delete the document.
*/
private LocalDateTime expireAfter;
public GroceryItem(String id, LocalDateTime expireAfter) {
super();
this.id = id;
this.expireAfter = expireAfter;
}
}
public persist(Item item) {
repository.save(new GroceryItem(item.getId(), getCorrectTTL(item.getExpirationDate(), item.getCategory())));
}
Подробнее здесь: https://stackoverflow.com/questions/773 ... annotation