使用新驱动程序 2.0 构建索引的新方法是什么? 没有任何关于此的文档。
显然这现在适用于新的 IndexKeysDefinitionBuilder<>
界面,但这就是我目前所得到的。
最佳答案
您需要使用 Builders.IndexKeys
获得的 IndexKeysDefinition
调用和 await
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));
}
如果你没有Hamster
,你也可以通过指定索引的json表示以非强类型的方式创建索引:
await collection.Indexes.CreateOneAsync("{ Name: 1 }");
关于c# - 使用 .NET 驱动程序 2.0 在 MongoDB 中构建索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29504425/