.net中mongodb全文搜索

时间:2023-02-17
本文介绍了.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全文搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:手动为 MongoDB 查询提供参数以支持整理功能(对于 下一篇:MongoDB C# 从组中获取最新文档

相关文章

最新文章