在 MongoDB 2.6.0 中使用 C# 驱动程序的 v1.9.0(在撰写本文时为最新版本)
目前通过 C# 驱动程序在集合上创建文本索引的最佳方法是什么?
据我所知,不可能通过 MongoCollection.CreateIndex
实现?所以目前使用 MongoDatabase.Eval 像这样创建它:
Database.Eval(new EvalArgs { Code = "function(){db.dummycollection.ensureIndex({\"$**\":\"text\"},{name:\"TextIndex\"});}"
我是否遗漏了什么/是否有更好的方法?
最佳答案
刚刚验证以下内容适用于 1.9.0 C# 驱动程序和 MongoDB 2.6.0-rc2:
MongoCollection.CreateIndex(new IndexKeysDocument("Markdown", "text"));
(这也适用于较旧的驱动程序)
编辑
djch 的回答显示了使用 1.9 驱动程序进行此操作的更好方法,因为它也可以使用 stronly-typed,例如:
MongoCollection.CreateIndex(IndexKeys<MyClass>.Text(p => p.Markdown));
关于mongodb - 通过 C# 驱动程序在 MongoDB 集合上创建文本索引的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23383322/