如何使用 TTL 创建 MongoDB 集合?
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 在其中创建集合或文档?
您的用户对象没有您在索引中指定的字段
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) }));
您可能还想接受 C# 和 MongoDB 驱动程序通过使用您的用户类型而不是 BsonDocument 为您提供的类型安全。下面是如何执行此操作的示例。
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 文档
中所述
The background task that removes expired documents runs every 60 seconds. As a result, documents may remain in a collection during the period between the expiration of the document and the running of the background task.
https://docs.mongodb.com/manual/core/index-ttl/#timing-of-the-delete-operation
我创建了一个对象集合
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 在其中创建集合或文档?
您的用户对象没有您在索引中指定的字段
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) }));
您可能还想接受 C# 和 MongoDB 驱动程序通过使用您的用户类型而不是 BsonDocument 为您提供的类型安全。下面是如何执行此操作的示例。
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 文档
中所述The background task that removes expired documents runs every 60 seconds. As a result, documents may remain in a collection during the period between the expiration of the document and the running of the background task. https://docs.mongodb.com/manual/core/index-ttl/#timing-of-the-delete-operation