问题
I just updated my MongoDB from version 2.5.0 to 2.7.0. Visual Studio tells me that the following way to create an index is obsolete:
protected override Task OnPerformMaintenanceAsync(CancellationToken cancellationToken) =>
NotificationLogs.Indexes.CreateOneAsync(Builders<NotificationLog>.IndexKeys.Ascending(_ => _.TimestampUtc));
It suggests me to use CreateIndexModel.
The only problem is that I cannot find an example to get this working that will do the same.
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);
}
But I get the following exception:
System.FormatException: 'JSON reader was expecting a value but found 'TimestampUtc'.'
回答1:
The new way in the MongoDB 2.7 driver is to do the following:
var notificationLogBuilder = Builders<NotificationLog>.IndexKeys;
var indexModel = new CreateIndexModel<NotificationLog>(notificationLogBuilder.Ascending(x => x.TimestampUtc));
await IMongoCollection.Indexes.CreateOneAsync(indexModel, cancellationToken: cancellationToken).ConfigureAwait(false);
回答2:
There is a not type safe method for a BsonDocument with the index options here:
var indexBuilder = Builders<BsonDocument>.IndexKeys;
var keys = indexBuilder.Ascending("timestamp");
var options = new CreateIndexOptions
{
Name = "expireAfterSecondsIndex",
ExpireAfter = TimeSpan.MaxValue
};
var indexModel = new CreateIndexModel<BsonDocument>(keys, options);
await collection.Indexes.CreateOneAsync(indexModel, cancellationToken: cancellationToken).ConfigureAwait(false);
回答3:
@StuiterSlurf can you help in updating this method with updated code.
private void CreateExpiryAfterIndex(IMongoCollection<BsonDocument> collection)
{
if (ExpireAfterSeconds <= 0) return;
var indexKeys = Builders<BsonDocument>.IndexKeys;
var options = new CreateIndexOptions{
Name = "expireAfterSecondsIndex",
ExpireAfter = new TimeSpan(ExpireAfterSeconds * TimeSpan.TicksPerSecond)
};
if (collection != null)
{
collection.Indexes.CreateOneAsync(keys: indexKeys.Ascending("timestamp"), options: options);
}
}
I have posted this question in another post which is marked for closing and refers me to check this question
回答4:
The following is working for me.
public async Task CreateIndexOnCollection(IMongoCollection<BsonDocument> collection, string field)
{
var keys = Builders<BsonDocument>.IndexKeys.Ascending(field);
await collection.Indexes.CreateOneAsync(keys);
}
or if we know what our indexes will be beforehand, we can use strongly typed implementation like this:
public async Task CreateIndexOnNameField()
{
var keys = Builders<User>.IndexKeys.Ascending(x => x.Name);
await _usersCollection.Indexes.CreateOneAsync(keys);
}
来源:https://stackoverflow.com/questions/51248295/mongodb-c-sharp-driver-create-index