我必须在 .net mvc 中搜索所有文档中的内容,特别是 mongodb 的集合.我已经通过像这里这样成功地创建索引来尝试使用 mongodb shell.
I have to search contents in all documents in particular collection of mongodb in .net mvc . I have tried with mongodb shell by creating index successfully like here .
db.collection_name.createIndex( { subject: "text" } )
db.collection_name.find( { $text: { $search: "search_word" } } )
它工作正常.但是当我把它放在 .net 中时,这给了我错误.我用谷歌搜索并得到了以下索引解决方案.
It works fine . but when i put it in .net that gives me error . I googled it and got following solution for indexing .
collection.EnsureIndex(new IndexKeysBuilder().Ascending("subject"));
现在我如何运行这个查询 db.collection_name.find( { $text: { $search: "coffee" } } )
.
now how can i run this query db.collection_name.find( { $text: { $search: "coffee" } } )
.
我正在 .net 中按以下方式尝试.
I am trying in .net as following way .
collection.CreateIndex("subject":"text");
var query = collection.Find({ $text: { $search: "coffe" }});
但我在第一行出现错误将文本表示为一系列 unicode ....语法错误"
but I am getting error on first line "represents text as series of unicode ....syntax error "
第二行错误没有给出与所需形式参数相对应的参数"和意外字符$".
2nd line error "There is no argument given that corresponds to required formal parameters " And "unexpected character $ ".
任何建议将不胜感激.
我可以用这个命令创建文本索引:
I could create text indexes with this command:
collection.Indexes.CreateOne(Builders<searchFileByAuthor>.IndexKeys.Text(x=>x.subject));
而且我可以这样查询索引:
And than i could query index this way:
collection.Find(Builders<searchFileByAuthor>.Filter.Text("coffe")).ToList();
searchFileByAuthor
只是我的带有主题字段的假类:
searchFileByAuthor
is just my fake class with subject field:
public class searchFileByAuthor
{
public int Id { get; set; }
public string subject { get; set; }
}
这篇关于.net中mongodb全文搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!