【发布时间】:2018-12-17 07:11:52
【问题描述】:
我刚刚将我的 MongoDB 从版本 2.5.0 更新到了 2.7.0。 Visual Studio 告诉我,以下创建索引的方法已过时:
protected override Task OnPerformMaintenanceAsync(CancellationToken cancellationToken)
=> NotificationLogs.Indexes
.CreateOneAsync(Builders<NotificationLog>.IndexKeys
.Ascending(_ => _.TimestampUtc));
它建议我使用CreateIndexModel。
唯一的问题是我找不到一个例子来让这个工作可以做同样的事情。
我试过了:
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'.'
【问题讨论】:
-
附注:“过时”通常是函数作者提供的属性。因此,告诉您这一点的不是 ReSharper,而是 IDE 本身。