c# – 在.net中的mongodb中进行全文搜索
发布时间:2020-12-15 18:36:25 所属栏目:百科 来源:网络整理
导读:我必须在.net mvc中搜索 mongodb特定集合中的所有文档中的内容.我已经通过像这里成功创建索引一样尝试了mongodb shell. db.collection_name.createIndex( { subject: "text" } )db.collection_name.find( { $text: { $search: "search_word" } } ) 它工作正
我必须在.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系列….语法错误” 第二行错误“没有给出对应于所需形式参数的参数”和“意外字符$”. 任何建议将不胜感激. 解决方法
我可以使用此命令创建文本索引:
collection.Indexes.CreateOne(Builders<searchFileByAuthor>.IndexKeys.Text(x=>x.subject)); 而且我可以这样查询索引: collection.Find(Builders<searchFileByAuthor>.Filter.Text("coffe")).ToList(); searchFileByAuthor只是我的主题字段的假类: public class searchFileByAuthor { public int Id { get; set; } public string subject { get; set; } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |