mcnamee mcnamee - 2 months ago 15
reST (reStructuredText) Question

FeathersJS REST Multiple Query String

With the FeathersJs REST client - how can I query a single field with multiple values?

Eg. if I have a Books service and I want to retrieve all books written in the year 1990, 1991 and 1992. I'd assume I'd call:

/books?year[]=1990&year[]=1991&year[]=1992


Although this doesn't work.

Answer

Have a look at the documentation for the common database adapter querying syntax. The correct way to query for what you are looking for is:

year: {
  $in: [ 1990, 1991, 1992 ]
}

The corresponding query would be

/books?year[$in]=1990&year[$in]=1991&year[$in]=1992

Parsing this query string requires using the extend query parser qs which you can enable with

app.set('query parser', 'extended');

Since the query is a string, if you need the actual number values for the year you might also have to convert it the query in a before hook (although most database usually allow both).