我創建了一個物件集合
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", "[email protected]");
collection.InsertOne(user.ToBsonDocument());
var user1 = new User("Petya", "[email protected]");
collection.InsertOne(user1.ToBsonDocument());
var user2 = new User("Kolya", "[email protected]");
collection.InsertOne(user2.ToBsonDocument());
count = collection.CountDocuments(new BsonDocument());
但是當我看到 http://localhost:8081/db/dbtest/ 時,我看不到任何 TTL 并且檔案不會在 30 秒后過期。
我做錯了什么?如何使用 TTL 在其中創建集合或檔案?
uj5u.com熱心網友回復:
您的用戶物件沒有您在索引中指定的欄位
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) }));
您可能還想通過使用您的 User 型別而不是 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", "[email protected]", DateTime.UtcNow);
collection.InsertOne(user);
var user1 = new User("Petya", "[email protected]", DateTime.UtcNow);
collection.InsertOne(user1);
var user2 = new User("Kolya", "[email protected]", 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/401457.html
標籤:C# MongoDB mongodb-.net-驱动程序
上一篇:在MongoDB查詢中
