static async Task CreateIndexAsync() { var client = new MongoClient(); var database = client.GetDatabase("HamsterSchool"); var collection = database.GetCollection<Hamster>("Hamsters"); var indexKeysDefinition = Builders<Hamster>.IndexKeys.Ascending(hamster => hamster.Name); await collection.Indexes.CreateOneAsync(new CreateIndexModel<Hamster>(indexKeysDefinition)); }
References
https://stackoverflow.com/questions/17807577/how-to-create-indexes-in-mongodb-via-net