使用mongodb csharp驱动程序2.0.0进行索引提示

hum*_*a17 3 mongodb mongodb-.net-driver

我正在从mongodb csharp驱动程序1.10.0迁移到2.0.0.我使用的其中一个集合非常大,必须使用不同的过滤器属性来完成许多查询.这就是我依赖一些索引提示语句的原因.使用v1.10驱动程序看起来像

 myCollection.Find(query).SetHint("myIndexName");
Run Code Online (Sandbox Code Playgroud)

我搜索了v2驱动程序api但是这个提示方法似乎在v2驱动程序中完全删除了.还有其他选择吗?我该如何使用v2驱动程序进行索引提示?

Cra*_*son 7

您可以使用FindOptions.Modifiers属性.

var modifiers = new BsonDocument("$hint", "myIndexName"); 
await myCollection.Find(query, new FindOptions { Modifiers = modifiers }).ToListAsync();
Run Code Online (Sandbox Code Playgroud)

请问你为什么要使用提示?服务器是否一直选择错误的索引?除特殊情况外,您不应该这样做.

克雷格