json-server icon indicating copy to clipboard operation
json-server copied to clipboard

How to filter by an array that contains a value

Open keviocastro opened this issue 8 years ago • 8 comments

Example:

GET HTTP://localhost/partners?segmentIds=1

I need segments 1 2 and 3, but it is only returning the segments with id 2 and 3

{
    "segments": [
        {
            "id": 1,
            "name": "Bear"
        },
        {
            "id": 2,
            "name": "Restaurant",
        },
        {
            "id": 3,
            "name": "Hamburgers",
        }
    ],
    "partners": [
        {
            "id": 1,
            "name": "Bear Head",
            "segmentIds": [1, 3]       
        },
        {
            "id": 2,
            "name": "Cariboos",
            "segmentIds": [1]
        },
        {
            "id": 3,
            "name": "Beer Beer",
            "segmentIds": [1],
        }
    ]
}

keviocastro avatar Oct 10 '17 17:10 keviocastro

Would also like to know if this is possible

jameelmoses avatar Oct 10 '17 18:10 jameelmoses

I just had to do this. You can use http://localhost:3000/partners?segmentIds_like=1

Key part is adding the _like

ericmccarthy7 avatar Oct 24 '17 19:10 ericmccarthy7

using like records that start with 1 are also returned. As for example:

"partners": [
        {
            "id": 121,
            "name": "XXXXXX",
            "segmentIds": [18]       
        }
 ]

is there any way to use filter with OR operator?

keviocastro avatar Nov 27 '17 13:11 keviocastro

that's a good opportunity for PR.

I'll try to do it soon.

Nilegfx avatar Dec 03 '17 01:12 Nilegfx

This feature would be really handy 👍

mrjasonlu avatar May 13 '18 11:05 mrjasonlu

👍 Yes - very useful feature!

boakenfull avatar May 22 '18 13:05 boakenfull

@ericmccarthy7 - your answer is helpful. Can you please assist how to configure the same in routes.json: https://stackoverflow.com/questions/52034326/mock-search-filter-using-json-server

hansiemithun avatar Aug 27 '18 07:08 hansiemithun

@Nilegfx did , we find any resolution for the same . didn't find any thing in official document . Like keyword is giving other results as well.

HimanshuMamodiya avatar Jun 30 '22 05:06 HimanshuMamodiya

Any update on this? It would be useful to be able to specify a _any or _all for searching within an array.

sneiland avatar Dec 11 '22 01:12 sneiland