MongoDB C# Driver Create Index

こ雲淡風輕ζ 提交于 2019-12-14 03:48:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!