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 .
我必须在。net mvc中搜索所有文档中的内容,特别是mongodb的集合。我尝试过mongodb shell,成功地创建了索引。
db.collection_name.createIndex( { subject: "text" } )
db.collection_name.find( { $text: { $search: "search_word" } } )
It works fine . but when i put it in .net that gives me error . I googled it and got following solution for indexing .
它将正常工作。但是当我把它放到。net时,它会给我错误。我搜索了一下,找到了索引的解决方案。
collection.EnsureIndex(new IndexKeysBuilder().Ascending("subject"));
now how can i run this query db.collection_name.find( { $text: { $search: "coffee" } } )
.
现在如何运行这个查询db.collection_name。查找({$text: {$search:“coffee”})。
I am trying in .net as following way .
我正在。net中尝试按照以下方式。
collection.CreateIndex("subject":"text");
var query = collection.Find({ $text: { $search: "coffe" }});
but I am getting error on first line "represents text as series of unicode ....syntax error "
但我得到错误第一行“将文本表示为一系列unicode ....语法错误”
2nd line error "There is no argument given that corresponds to required formal parameters " And "unexpected character $ ".
第2行错误“没有给出对应于所需形式参数的参数”和“意外字符$”。
any suggestion will be appreciated .
如有任何建议,我们将不胜感激。
2 个解决方案
#1
3
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
is just my fake class with subject field:
searchFileByAuthor是我的伪类,包含主题字段:
public class searchFileByAuthor
{
public int Id { get; set; }
public string subject { get; set; }
}
#2
0
public List<T> FindSearch<T>(string collectionName, string searchWord) {
IMongoQuery query = Query.Text(searchWord);
List<T> find = getCollection<T>(collectionName).Find(query).ToList();
return find;
}
#1
3
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
is just my fake class with subject field:
searchFileByAuthor是我的伪类,包含主题字段:
public class searchFileByAuthor
{
public int Id { get; set; }
public string subject { get; set; }
}
#2
0
public List<T> FindSearch<T>(string collectionName, string searchWord) {
IMongoQuery query = Query.Text(searchWord);
List<T> find = getCollection<T>(collectionName).Find(query).ToList();
return find;
}