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全文搜索
基础教程推荐
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- WPF 模态进度窗口 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
