通过 C# 驱动程序在 MongoDB 集合上创建文本索引的最佳方法

Ada*_*Dev 6 mongodb mongodb-.net-driver

将 C# 驱动程序 v1.9.0(撰写本文时最新)与 MongoDB 2.6.0 结合使用

当前通过 C# 驱动程序在集合上创建文本索引的最佳方法是什么?

据我所知,这是不可能的MongoCollection.CreateIndex?因此,目前使用 MongoDatabase.Eval 创建它,如下所示:

Database.Eval(new EvalArgs { Code = "function(){db.dummycollection.ensureIndex({\"$**\" : \"text\"},{name:\"TextIndex\"});}"

我错过了什么/有更好的方法吗?

mne*_*syn 5

刚刚验证以下内容是否适用于 1.9.0 C# 驱动程序和 MongoDB 2.6.0-rc2:

MongoCollection.CreateIndex(new IndexKeysDocument("Markdown", "text"));
Run Code Online (Sandbox Code Playgroud)

(这也适用于老司机)

编辑

djch 的答案显示了使用 1.9 驱动程序执行此操作的更好方法,因为它也可以使用强类型,例如:

MongoCollection.CreateIndex(IndexKeys<MyClass>.Text(p => p.Markdown));
Run Code Online (Sandbox Code Playgroud)


Dav*_*son 3

这应该有效:

collection.EnsureIndex(IndexKeys.Text("a", "b").Ascending("c"), IndexOptions.SetTextLanguageOverride("idioma").SetName("custom").SetTextDefaultLanguage("spanish"));
Run Code Online (Sandbox Code Playgroud)

https://jira.mongodb.org/browse/CSHARP-874

https://github.com/mongodb/mongo-csharp-driver/commit/1e7db3bedb3bee1b0ccecdb5f8ff39854526213a