How do you filter Strapi results on properties multi levels deep?

478 Views Asked by At

I am trying to filter results from Strapi based on fields that are multi levels deep and I can't get it to work. A find all on something returns this:

{
"data": [
    {
        "id": 1,
        "attributes": {
            "Name": "Batch Brewing Company",
            "Address": "44 Sydenham Rd Marrickville NSW 2204",
            "Phone": "(02) 9550 5432",
            "Location": {
                "geohash": "r3grpfdckvw1",
                "coordinates": {
                    "lat": -33.9118039304194,
                    "lng": 151.16487711668017
                }
            },
            "createdAt": "2023-08-09T08:03:49.382Z",
            "updatedAt": "2023-08-09T08:03:52.884Z",
            "publishedAt": "2023-08-09T08:03:52.877Z"
        }
    },
    {
        "id": 2,
        "attributes": {
            "Name": "Willie the Boatman",
            "Address": "75 Mary St St Peters NSW 2044",
            "Phone": "0285567528",
            "Location": {
                "geohash": "r3gx042sxfhj",
                "coordinates": {
                    "lat": -33.91263754768333,
                    "lng": 151.1728996038437
                }
            },
            "createdAt": "2023-08-09T08:15:19.860Z",
            "updatedAt": "2023-08-09T08:15:21.024Z",
            "publishedAt": "2023-08-09T08:15:21.022Z"
        }
    }
],
"meta": {
    "pagination": {
        "page": 1,
        "pageSize": 25,
        "pageCount": 1,
        "total": 2
    }
}
}

If I filter on anything in the top level it works. Name for example. Even if I filter Location on a property inside it, that works. For example: http://127.0.0.1:1337/api/distributors?filters[Location][$contains]=r3grpfdckvw1

But when I try to filter on the properties below Location it doesn't work. This returns all results: http://127.0.0.1:1337/api/distributors?filters[Location][geohash][$contains]=r3grpfdckvw1

How can I filter based on deeper level properties?

I'm using Nuxt Strapi so I'm also trying it with that I have the same thing.

This works:

const { find } = useStrapi();
const response = await find('distributors', {filters: {Location: {$null: true}}  });

This doesn't work:

const { find } = useStrapi();
const response = await find('distributors', {filters: {Location: {geohash: {$null: true}}}});
1

There are 1 best solutions below

0
Mo Mansour On

You have to populate whatever nested property you're trying to filter or access.

By default, Strapi does not populate relations, components, media files, or dynamic zones. If you want to populate the Location component, you can add populate=Location, which will only populate the first level of Location, and you can populate nested properties like this populate=Location.coordinates or using LHS Brackets Notation which is recommended by Strapi. See Strapi docs for populating and deep populating examples

Now since you want to filter by Location.geohash, you have to populate it first with populate[0]=Location.
Here's how your request URL would look like:
http://127.0.0.1:1337/api/distributors?populate[0]=Location&filters[Location][geohash][$contains]=r3grpfdckvw1