![](/img/trans.png)
[英]How to create MongoDB TTL Index for DatetimeOffset stored as an array [ticks,offset]?
[英]How to create MongoDB collection with TTL?
我創建了一個對象集合
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 在其中創建集合或文檔?
您的用戶 object 沒有您在索引中指定的字段
public class User
{
public User(string fullname, string email, DateTime lastModifiedDate)
{
Fullname = fullname;
Email = email;
LastModifiedDate = lastModifiedDate;
}
public string Fullname { get; }
public string Email { get; }
public DateTime LastModifiedDate { get; }
}
此外,MongoDB C# 驅動程序中序列化的默認約定不是駱駝大小寫,因此您需要將索引更新為以下內容:
var collection = _database.GetCollection("bar");
collection.Indexes.CreateOne(new CreateIndexModel
(new BsonDocument("LastModifiedDate", 1), new CreateIndexOptions { ExpireAfter = new TimeSpan(0, 0, 30) }));
您可能還希望通過使用用戶類型而不是 BsonDocument 來接受 C# 和 MongoDB 驅動程序為您提供的類型安全。 以下是如何執行此操作的示例。
using MongoDB.Driver;
var client = new MongoClient();
var database = client.GetDatabase("test");
var collection = database.GetCollection<User>("users");
await collection.Indexes.CreateOneAsync(
Builders<User>.IndexKeys.Ascending(x => x.LastModifiedDate),
new CreateIndexOptions { ExpireAfter = new TimeSpan(0, 0, 30) });
var user = new User("Vasya", "billgates@ms.com", DateTime.UtcNow);
collection.InsertOne(user);
var user1 = new User("Petya", "Petya@ms.com", DateTime.UtcNow);
collection.InsertOne(user1);
var user2 = new User("Kolya", "Kolya@ms.com", DateTime.UtcNow);
collection.InsertOne(user2);
for (var i = 0; i < 10; i++)
{
var count = await collection.CountDocumentsAsync(Builders<User>.Filter.Empty);
Console.WriteLine($"Count: {count} @ {DateTime.UtcNow}");
await Task.Delay(10000);
}
// Count: 4 @ 02/01/2022 11:42:13
// Count: 4 @ 02/01/2022 11:42:23
// Count: 4 @ 02/01/2022 11:42:33
// Count: 4 @ 02/01/2022 11:42:43
// Count: 4 @ 02/01/2022 11:42:53
// Count: 1 @ 02/01/2022 11:43:03
// Count: 1 @ 02/01/2022 11:43:13
// Count: 1 @ 02/01/2022 11:43:23
// Count: 1 @ 02/01/2022 11:43:33
// Count: 0 @ 02/01/2022 11:43:43
public class User
{
public User(string fullname, string email, DateTime lastModifiedDate)
{
Fullname = fullname;
Email = email;
LastModifiedDate = lastModifiedDate;
}
public string Fullname { get; }
public string Email { get; }
public DateTime LastModifiedDate { get; }
}
您可能會注意到文檔不會立即被刪除,但正如 MongoDB 文檔中所述
刪除過期文檔的后台任務每 60 秒運行一次。 因此,在文檔到期和后台任務運行之間的時間段內,文檔可能會保留在集合中。 https://docs.mongodb.com/manual/core/index-ttl/#timing-of-the-delete-operation
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.