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

标签 mongodb mongodb-.net-driver

在 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/

相关文章:

javascript - NodeJs MongoDB 嵌套查询 - 同步运行回调

MongoDB C# 连接/断开连接(官方驱动)

MongoDB - 备份和恢复用户和角色

javascript - 在 Meteor 应用程序中将名称映射到 id

java - mongodb spring boot 的日期分页

javascript - 从嵌入式文档中删除字段

c# - 带有聚合管道的 MongoDB C# 驱动程序更新文档

javascript - _ids 数组的 MongoDB 请求(这可能吗?)

c# - 如何在 MongoDB C# Driver 2.0 中记录我的查询?

mongodb - 在 mongodb c# 驱动程序中设置 DateTimeSerializationOptions.Defaults 的新方法是什么?