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?
– Lucas Miranda
I updated the code from a look there I put my Itemservice class
– Robson Junior
your item entity is with the right annotations?
– Lucas Miranda
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
– Robson Junior