我刚刚将 MongoDB 从版本 2.5.0 更新到 2.7.0。 Visual Studio 告诉我以下创建索引的方法已过时:
protected override Task OnPerformMaintenanceAsync(CancellationToken cancellationToken)
=> NotificationLogs.Indexes
.CreateOneAsync(Builders<NotificationLog>.IndexKeys
.Ascending(_ => _.TimestampUtc));
它建议我使用创建索引模型 https://mongodb.github.io/mongo-csharp-driver/2.6/apidocs/html/T_MongoDB_Driver_CreateIndexModel_1.htm.
唯一的问题是我找不到一个可以实现相同功能的示例。
I tried:
protected Task OnPerformMaintenanceTestAsync(CancellationToken cancellationToken)
{
// Old approach
var builder = Builders<NotificationLog>.IndexKeys
.Ascending(x => x.TimestampUtc);
// New approach
var indexModel = new CreateIndexModel<NotificationLog>(nameof(NotificationLog.TimestampUtc));
return NotificationLogs.Indexes.CreateOneAsync(indexModel);
}
但我得到以下异常:
System.FormatException: 'JSON reader was expecting a value but found 'TimestampUtc'.'
MongoDB 2.7 驱动程序中的新方法是执行以下操作:
var notificationLogBuilder = Builders<NotificationLog>.IndexKeys;
var indexModel = new CreateIndexModel<NotificationLog>(notificationLogBuilder.Ascending(x => x.TimestampUtc));
// .NET Full framwork:
// .NET Standard library:
await IMongoCollection.Indexes
.CreateOneAsync(indexModel, cancellationToken: cancellationToken)
.ConfigureAwait(false);
// .NET Core:
await IMongoCollection.Indexes
.CreateOneAsync(indexModel, cancellationToken: cancellationToken)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)