Full text search in mongodb in .net(.net中mongodb全文搜索)
问题描述
我必须在 .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全文搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:.net中mongodb全文搜索


- 在 C# 中异步处理项目队列 2022-01-01
- 使用 rss + c# 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01