与.NET 2.0的驱动程序建立索引在MongoDB中 [英] Building indexes in MongoDB with .NET driver 2.0

查看:360
本文介绍了与.NET 2.0的驱动程序建立索引在MongoDB中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是新的方式来与新的驱动程序2.0建立索引? 有没有任何文档关于这个。

What's the new way to build indexes with the new driver 2.0? There's no documentation whatsoever about this.

显然,这个现在可以用新的 IndexKeysDefinitionBuilder<> 接口,但是这是我的一切,到目前为止

Apparently this now works with the new IndexKeysDefinitionBuilder<> interface but that's all I got so far.

推荐答案

您需要打电话等待 CreateOneAsync IndexKeysDefinition 您使用得到 Builders.IndexKeys

You need to call and await CreateOneAsync with an IndexKeysDefinition you get by using Builders.IndexKeys:

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重新presentation创建索引在非强类型的方式:

If you don't have a Hamster you can also create the index in a non-strongly-typed way by specifying the index's json representation:

await collection.Indexes.CreateOneAsync("{ Name: 1 }");

这篇关于与.NET 2.0的驱动程序建立索引在MongoDB中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆