diegohaz/rest

Query search option not working

mveron opened this issue · 4 comments

Hi Diego

Im trying to fetch custom data with the /q option in the API but after trying almost every possible option I wasn´t able to retrieve any data. Can you provide an example of how to run a search based on a specific field?. Thanks in advanced.

url: http://localhost:9000/test?q=...

Hi, @mveron. It should be pretty easy. Which options have you tested?

Hi Diego

thanks for the quick response. I´m using the q option available in the documentation created after running 'npm run docs'.

I have a collection containing information about devices in the network and I would like to query for a particular device or devices with "something" in their name key . I ´m passing parameters in the GET request as "/?q=name=pc01 but no matter what I tried I got an empty array so after several attempts Im wondering about the right sintax to get all records with "pc-01" in the name key in the json example below. I have a db with +10K records and searching by ID is not efficient in this case. Thanks.

JSON EXample:
{
name: pc-01-contoso,
dns: pc01.contoso.com
}
{
name: pc-02-contoso,
dns: pc02.consoto.com

}

@mveron looks like you need to specify custom schema for querymen for get('/') like this - this is what worked for me:

router.get('/',
  query({
    name: {
      type: String,
      operator: '$regex'
    }
  }),
  index)

Here name is the field you want to filter by. Then url like this works: http://localhost:9000/test?name=^pc-01.

Thank you very much @artemv it worked perfectly!. I appreciate your help guys 👍