Search mongodb by text?

Asked

Viewed 435 times

0

I want to create a search in the items I am listing, I want to filter according to what user pass in a field type google.com click search and return the results that contain that text, but I want you to search everything that contains that text even though I have things before and even I have things after without case sensitive.

I am using Web API with ASP.NET Core and Mongodb

I tried codes like this, but they are code but for cmd, I want to use in my service class that makes the data persistence

        //return _items.Find<Item>(item => item.Name == LIKE keyword).FirstOrDefault();
        //return _items.Find<Item>(item => item.Name == itemIn.Name).FirstOrDefault();
        //return _items.find({$text: {$search: "Sorriso"} });

To connect to Mongo I’m using this class

using desafio.Models;

using Microsoft.Extensions.Configuration; using Mongodb.Driver; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks;

namespace challenge. Services { public class Itemservice { private readonly Imongocollection _items;

    public ItemService(IConfiguration config)
    {
        var client = new MongoClient(config.GetConnectionString("DesafioDb"));
        var database = client.GetDatabase("DesafioDb");
        _items = database.GetCollection<Item>("Items");
    }

    public List<Item> Get()
    {
        return _items.Find(item => true).ToList();
    }

    public Item Get(string id)
    {
        return _items.Find<Item>(item => item.Id == id).FirstOrDefault();
    }

    public List<Item> Buscar(string keyword)
    {
        //Colocar verificação se keyword for vazinho buscar o metodo get que retorna tudo ou alaramr que não foi encontrado

        Item itemIn = new Item();
        itemIn.Name = keyword;

        return _items.Find(item => item.Name == itemIn.Name).ToList();
    }

    public Item Create(Item item)
    {
        _items.InsertOne(item);
        return item;
    }

    public void Update(string id, Item itemIn)
    {
        _items.ReplaceOne(item => item.Id == id, itemIn);
    }

    public void Remove(Item itemIn)
    {
        _items.DeleteOne(item => item.Id == itemIn.Id);
    }

    public void Remove(string id)
    {
        _items.DeleteOne(item => item.Id == id);
    }
}

}

  • what you are using to make connection with Mongo?

  • I updated the code from a look there I put my Itemservice class

  • your item entity is with the right annotations?

  • Yes everything is working fine but in the method Search I only search if the word is right, I want to do those search type in sql that no matter what comes before or after

1 answer

2

Containing the text you need to use the famous relational database like. Follow examples in Mongo.

inserts = 

db.users.insert({name: 'paulo'});

db.users.insert({name: 'patric'})

db.users.insert({name: 'pedro'})

pesquisas =

db.users.find({name: /a/})  //like '%a%'
saída: paulo, **patric**

db.users.find({name: /^pa/}) //like 'pa%' 
saída: **paulo, patric**

db.users.find({name: /ro$/}) //like '%ro'
saída: **pedro**

//return _items.find({$text: {$search: /Sorriso/} }); que contenham a palavra  Sorriso
  • But these commands only work in the shell, they don’t work in the class I implement the search method

Browser other questions tagged

You are not signed in. Login or sign up in order to post.