@R_403_323@
您需要通过使用Builders.IndexKeys获得的IndexKeysDefinition来调用和等待CreateOneAsync:
static async Task CreateIndex() { var client = new MongoClient(); var database = client.GetDatabase("db"); var collection = database.GetCollection<Hamster>("collection"); await collection.Indexes.CreateOneAsync(Builders<Hamster>.IndexKeys.Ascending(_ => _.Name)); }
如果您没有仓鼠,您还可以通过指定索引的json表示形式以非强类型的方式创建索引:
await collection.Indexes.CreateOneAsync("{ Name: 1 }");