Search code examples
predicatecommercetoolssphere.io

Commercetools - Use query predicates to filter on a collection attribute


I'd like to get all the Category items that have between their ancestors an ancestor with a certain "id".

Here is the JSON of one of the categories returned by GETting from /categories:

{
        "id": "4627f3b0-fe52-4cc6-b03e-3fd72e701342",
        "version": 1,
        "lastMessageSequenceNumber": 1,
        "createdAt": "2019-02-18T13:48:51.677Z",
        "lastModifiedAt": "2019-02-18T13:48:51.677Z",
        "lastModifiedBy": {
            "clientId": "_anonymous"
        },
        "createdBy": {
            "clientId": "_anonymous"
        },
        "key": "snowboard-gloves",
        "name": {
            "en": "Snowboard Gloves"
        },
        "slug": {
            "en": "snowboard-gloves"
        },
        "description": {
            "en": "Gloves specifically designed for snowboarding"
        },
        "ancestors": [
            {
                "typeId": "category",
                "id": "b27086d2-33f2-43c3-aad1-4c01b2b9a886"
            }
        ],
        "parent": {
            "typeId": "category",
            "id": "b27086d2-33f2-43c3-aad1-4c01b2b9a886"
        },
        "orderHint": "0.000016",
        "metaTitle": {
            "en": "Snowboard Gloves"
        },
        "metaDescription": {
            "en": "Gloves specifically designed for snowboarding"
        },
        "assets": []
    }

I'd like to call the /categories API with a where clause on ancestors[x].id = "b27086d2-33f2-43c3-aad1-4c01b2b9a886" but from the documentation I don't understand how I should write the query predicate.

Can anyone help me?


Solution

  • The query predicate follows the structure of the json response. Nested fields are accessed with () brackets.

    Try this out

    ancestors(id = "idb27086d2-33f2-43c3-aad1-4c01b2b9a886")