在.net中的mongodb中进行全文搜索

我必须在.net mvc中搜索mongodb特定集合中的所有文档中的内容。 我已经通过像这里成功创建索引一样尝试了mongodb shell。

db.collection_name.createIndex( { subject: "text" } ) db.collection_name.find( { $text: { $search: "search_word" } } ) 

它工作正常。 但是,当我把它放在.net中,这给了我错误。 我用Google搜索并获得了索引的以下解决方案。

  collection.EnsureIndex(new IndexKeysBuilder().Ascending("subject")); 

现在我怎么能运行这个查询db.collection_name.find( { $text: { $search: "coffee" } } )

我按照以下方式尝试.net。

 collection.CreateIndex("subject":"text"); var query = collection.Find({ $text: { $search: "coffe" }}); 

但我在第一行收到错误“表示文本为unicode系列….语法错误”

第二行错误“没有给出对应于所需forms参数的参数”和“意外字符$”。

任何建议将不胜感激。

我可以使用此命令创建文本索引:

 collection.Indexes.CreateOne(Builders.IndexKeys.Text(x=>x.subject)); 

而且我可以这样查询索引:

 collection.Find(Builders.Filter.Text("coffe")).ToList(); 

searchFileByAuthor只是我的主题字段的假类:

 public class searchFileByAuthor { public int Id { get; set; } public string subject { get; set; } } 
 public List FindSearch(string collectionName, string searchWord) { IMongoQuery query = Query.Text(searchWord); List find = getCollection(collectionName).Find(query).ToList(); return find; }