【发布时间】:2022-01-02 10:28:01
【问题描述】:
我创建了一个对象集合
public class User
{
public User(string fullname, string email)
{
Fullname = fullname;
Email = email;
}
public string Fullname { get; }
public string Email { get; }
}
并创建索引和一些文档
var collection = _database.GetCollection(“bar”);
collection.Indexes.CreateOne(new CreateIndexModel
(new BsonDocument(“lastModifiedDate”, 1), new CreateIndexOptions { ExpireAfter = new TimeSpan(0, 0, 30) }));
var user = new User("Vasya", "billgates@ms.com");
collection.InsertOne(user.ToBsonDocument());
var user1 = new User("Petya", "Petya@ms.com");
collection.InsertOne(user1.ToBsonDocument());
var user2 = new User("Kolya", "Kolya@ms.com");
collection.InsertOne(user2.ToBsonDocument());
count = collection.CountDocuments(new BsonDocument());
但是当我看到 http://localhost:8081/db/dbtest/ 然后我看不到任何 TTL 并且文档在 30 秒后不会过期。
我做错了什么?如何使用 TTL 在其中创建集合或文档?
【问题讨论】:
-
TTL 工作器仅每 60 秒运行一次,因此删除这些文档最多可能需要 90 秒。
-
好的,我又测试了,等了2分钟;没有效果,集合还活着...可能我需要在其上创建索引过期的字段 lastModifiedDate 吗?
-
如果 lastModifiedDate 不包含日期类型,文档将永远不会被删除。
标签: c# mongodb mongodb-.net-driver