Full text search at mongodb in.net

I need to search the contents in all documents, in particular in the mongodb collection in .net mvc. I tried using the mongodb shell by creating the index successfully, as here.

db.collection_name.createIndex( { subject: "text" } )

db.collection_name.find( { $text: { $search: "search_word" } } )

It works great. but when I put it in .net, it gives me an error. I searched for it and got the following indexing solution.

 collection.EnsureIndex(new IndexKeysBuilder().Ascending("subject"));

now how can i run this query db.collection_name.find( { $text: { $search: "coffee" } } ).

I am trying to use .net as follows.

collection.CreateIndex("subject":"text");

var query = collection.Find({ $text: { $search: "coffe" }}); 

but I get an error in the first line "presents the text as a unicode series .... syntax error"

2nd line error "There are no arguments that match the required formal parameters" AND "unexpected symbol $".

Any suggestion would be appreciated.

+4
2

:

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; } 
}
+1
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;
}
0

Source: https://habr.com/ru/post/1665072/


All Articles